前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >我的tkinter学习笔记

我的tkinter学习笔记

作者头像
用户6367961
发布2020-04-07 11:04:56
6700
发布2020-04-07 11:04:56
举报
文章被收录于专栏:自学测试之道

python结合tkinter,可以开发出我们想要的小工具,从而在工作上帮我们提供工作效率。比如,开发一个一键获取APP的包名和Activity等等。下面我们一起先记录下tkinter的基本操作。

1、tkinter小窗口及标题

代码语言:javascript
复制
#coding:utf-8

import tkinter as tk

# app是一个Tk(界面)类
app = tk.Tk()
app.title("标题")

# the label是一个Label类
theLabel = tk.Label(app, text="我的第一个tkinter标签")  # 建立一个label类
theLabel.pack()

app.mainloop()

2、tkinter绑定事件

代码语言:javascript
复制
#coding:utf-8

from tkinter import *

def p_label():
    global root
    Lb = Label(root, text='我爱学习')
    Lb.pack()

root = Tk()
root.title("应用程序窗口")
B_n = Button(root, text='点我', command=p_label, bg='red')  # command后面不能有任何的标点符号
B_n.pack()
root.mainloop()

3、tkinter布局显示

代码语言:javascript
复制
#coding:utf-8

from tkinter import *
root = Tk()
root.title("应用程序窗口")
Button(root,text='1').pack(side=LEFT,expand=YES,fill=Y)
Button(root,text='2').pack(side=TOP,expand=YES,fill=BOTH)
Button(root,text='3').pack(side=RIGHT,expand=YES,fill=NONE)
Button(root,text='4').pack(side=LEFT,expand=NO,fill=Y)
Button(root,text='5').pack(side=TOP,expand=YES,fill=BOTH)
Button(root,text='6').pack(side=BOTTOM,expand=YES)
Button(root,text='7').pack(anchor=SE)
root.mainloop()

4、tkinter图片显示和button切换

代码语言:javascript
复制
#coding:utf-8
# 插入文件图片
import tkinter as tk

root = tk.Tk()
root.title("应用程序窗口")
frame1 = tk.Frame(root)  # 这是上面的框架
frame2 = tk.Frame(root)  # 这是下面的框架

var = tk.StringVar()  # 储存文字的类
var.set("你在右边会看到一个图片,\n我在换个行")  # 设置文字

# 创建一个标签类, [justify]:对齐方式,[frame]所属框架
textLabel = tk.Label(frame1, textvariable=var,justify = tk.LEFT)  # 显示文字内容
textLabel.pack(side=tk.LEFT)  # 自动对齐,side:方位

# 创建一个图片管理类
photo = tk.PhotoImage(file="gzh.gif")  # file:t图片路径
imgLabel = tk.Label(frame1, image=photo)  # 把图片整合到标签类中
imgLabel.pack(side=tk.RIGHT)  # 自动对齐

def callback():  # 触发的函数
    var.set("你还真按了")  # 设置文字

# [frame]所属框架 ,text 文字内容 command:触发方法
theButton = tk.Button(frame2, text="我是下面的按钮", command=callback)
theButton.pack()  # 自动对齐
frame1.pack(padx=10, pady=10)  # 上框架对齐
frame2.pack(padx=10, pady=10)  # 下框架对齐
tk.mainloop()

5、tkinter之grid网格布局

代码语言:javascript
复制
#coding:utf-8

from tkinter import *

root = Tk()
root.title("应用程序窗口")
Label(root, text='用户名:').grid(row=0, sticky=W)
Entry(root).grid(row=0, column=1, sticky=E)
Label(root, text='密码:').grid(row=1, sticky=W)
Entry(root).grid(row=1, column=1, sticky=E)  # 输入框
Button(root, text='登陆').grid(row=2, column=1, sticky=E)

root.mainloop()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-03-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 自学测试之道 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档