Python中的tkinter库简单案例详解
程序员文章站
2022-06-17 21:29:55
目录案例一label & button 标签和按钮案例二entry & text 输入和文本框案例三listbox 部件案例四radiobutton 选择按钮案例五scale 尺度案例...
tkinter
python 的 gui 库非常多,之所以选择 tkinter,一是最为简单,二是自带库,不需下载安装,随时使用,跨平台兼容性非常好,三则是从需求出发的,python 在实际应用中极少用于开发复杂的桌面应用,毕竟,python 的各种 gui 工具包都“一般得很”,不具备优势。
案例一
label & button 标签和按钮
import tkinter as tk window = tk.tk() # 实例化tk window.title("muy window") # 设置标题 window.geometry("200x100") # 设置窗口的大小 var1 = tk.stringvar() # 定义tk变量 l1 = tk.label(window, textvariable=var1, bg="green", font=("arial", 12), width=15, height=2) # 设置标签,内容显示为var1变量 l1.pack() # 摆放这个标签 hit_me1 = false def hit_me(): global hit_me1 if not hit_me1: hit_me1 = true var1.set("this is tk") # 给变量赋值 else: hit_me1 = false var1.set("this is tk, too") b1 = tk.button(window, text="hit me", width=15, height=2, command=hit_me) # 点击按钮,运行hit_me这个函数 b1.pack() window.mainloop() # 显示窗口
案例二
entry & text 输入和文本框
import tkinter as tk window = tk.tk() # 实例化tk window.title("muy window") # 设置标题 window.geometry("200x150") # 设置窗口的大小 e = tk.entry(window, show=none) # 如果输入密码的话,show="*" e.pack() def insert_point(): var = e.get() # 得到文本输入框的值 t.insert("insert", var) # 文本放到光标位置 def insert_end(): var = e.get() t.insert("end", var) # 将文本放到文本框的末尾 # t.insert(1.1, var) # 将文本放到第一行第一列 b1 = tk.button(window, text="insert point", width=15, height=2, command=insert_point).pack() # 定义一个按钮,显示在光标位置插入文本 b2 = tk.button(window, text="insert end", width=15, height=2, command=insert_end).pack() # 在末尾出插入 t = tk.text(window, height=2, width=15) t.pack() # 注意不能一行写完,pack要写在下一行 window.mainloop() # 显示窗口
案例三
listbox 部件
import tkinter as tk window = tk.tk() # 实例化tk window.title("muy window") # 设置标题 window.geometry("200x150") # 设置窗口的大小 var1 = tk.stringvar() l = tk.label(window, bg="yellow", width=4, textvariable=var1) l.pack() def print_selection(): var = lb.get(lb.curselection()) # 得到鼠标点击的内容 var1.set(var) # 将值赋值给var1 b1 = tk.button(window, text="insert point", width=15, height=2, command=print_selection).pack() # 定义一个按钮,显示在光标位置插入文本 var2 = tk.stringvar() # 存储可迭代对象的值 var2.set((1, 2, 3, 4)) # 列表也可,设置初始值 lb = tk.listbox(window, listvariable=var2) list_item = [1, 2, 11, 54] for i in list_item: lb.insert("end", i) # 向列表框中插入值 lb.delete(1) # 删除下标索引为2的值 lb.pack() window.mainloop() # 显示窗口
案例四
radiobutton 选择按钮
import tkinter as tk window = tk.tk() # 实例化tk window.title("muy window") # 设置标题 window.geometry("200x150") # 设置窗口的大小 var1 = tk.stringvar() l = tk.label(window, bg="yellow", width=15) l.pack() def print_selection(): l.config(text=f"you have select {var1.get()}") # 将text重新赋值 rad1 = tk.radiobutton(window, text="select a", variable=var1, value="a", command=print_selection).pack() # variable=var1, value="a" 的意思是,将var1赋值成a rad2 = tk.radiobutton(window, text="select b", variable=var1, value="b", command=print_selection).pack() rad3 = tk.radiobutton(window, text="select c", variable=var1, value="c", command=print_selection).pack() window.mainloop() # 显示窗口
案例五
scale 尺度
import tkinter as tk window = tk.tk() # 实例化tk window.title("muy window") # 设置标题 window.geometry("200x150") # 设置窗口的大小 l = tk.label(window, bg="yellow", width=15) l.pack() def print_selection(v): # scale调用函数,其有自动的传入值 l.config(text=f"you have select {v}") s = tk.scale(window, label="try me", from_=5, to=11, orient=tk.horizontal, length=200, showvalue=0, tickinterval=3, resolution=0.01, command=print_selection) # label设置名字,from to 设置刻度,orient设置方向,length设置长度,showvalue展示的值,tickinterval刻度,resolution精度 s.pack() window.mainloop() # 显示窗口
案例六
checkbutton 勾选项
import tkinter as tk window = tk.tk() # 实例化tk window.title("muy window") # 设置标题 window.geometry("200x150") # 设置窗口的大小 l = tk.label(window, bg="yellow", width=15, text="empty") l.pack() def print_selection(): # scale调用函数,其有自动的传入值 if (var1.get() == 1) & (var2.get() == 0): l.config(text="i love python") elif (var1.get() == 1) & (var2.get() == 1): l.config(text="i love both") elif (var1.get() == 0) & (var2.get() == 1): l.config(text="i love c++") else: l.config(text="i don't love either") var1 = tk.intvar() var2 = tk.intvar() cl = tk.checkbutton(window, text="python", variable=var1, onvalue=1, offvalue=0 , command=print_selection) # 选择var1的值为1,否则为0 c2 = tk.checkbutton(window, text="c++", variable=var2, onvalue=1, offvalue=0 , command=print_selection) cl.pack() c2.pack() window.mainloop() # 显示窗口
案例七
canvas 画布
import tkinter as tk window = tk.tk() # 实例化tk window.title("muy window") # 设置标题 window.geometry("200x200") # 设置窗口的大小 canvas = tk.canvas(window, bg="blue", height=100, width=200) image_file = tk.photoimage(file=r"d:\programme\web\web_file\web学习\images\image01.jpg") # 注意,只能导入.gif的图片 image = canvas.create_image(0, 0, anchor="nw", image=image_file).pack() # 锚点为左上角,西北方向 x0, y0, x1, y1 = 20, 50, 80, 80 line = canvas.create_line(x0, y0, x1, y1) # 画线 oval = canvas.create_oval(x0, y0, x1, y1, fill="red") # 绘制扇形 arc = canvas.create_arc(x0 + 30, y0 + 30, x1 + 30, x1 + 30, y1 + 30, start=0, extent=180) # 扇形 rect = canvas.create_rectangle(100, 30, 100 + 20, 30 + 20) # 绘制正方形 canvas.pack() def moveit(): canvas.move(rect, 0, 2) # 移动正方形,使其往下走两个单位 a = tk.button(window, text="move", command=moveit).pack() window.mainloop() # 显示窗口
案例八
menubar 菜单
import tkinter as tk window = tk.tk() # 实例化tk window.title("my window") # 设置标题 window.geometry("200x200") # 设置窗口的大小 count = 0 l = tk.label(window, text="", bg="yellow") l.pack() def do_job(): global count l.config(text=f"do{str(count)}") count += 1 """定义文件选项""" mb = tk.menu(window) file = tk.menu(mb, tearoff=0) # 有没有分隔线 mb.add_cascade(label="file", menu=file) # 添加一个功能 file.add_command(label="new", command=do_job) # 每一个选项都是一个功能,命令 file.add_command(label="open", command=do_job) file.add_command(label="save", command=do_job) sm = tk.menu(file) # 在file里面继续添加命令 file.add_cascade(label="submit", menu=sm) sm.add_command(label="new", command=do_job) window.config(menu=mb) # 将菜单添加到window上面 window.mainloop() # 显示窗口
案例九
frame 框架
import tkinter as tk window = tk.tk() # 实例化tk window.title("my window") # 设置标题 window.geometry("200x200") # 设置窗口的大小 tk.label(window, text="on the window").pack() # 添加框架 fm = tk.frame(window) fm.pack() fm_1 = tk.frame(fm) fm_r = tk.frame(fm) fm_1.pack(side="left") fm_r.pack(side="right") tk.label(fm_1, text="on the frame_1").pack() tk.label(fm_r, text="on the frame_r").pack() window.mainloop() # 显示窗口
案例十
messagebox 弹窗
import tkinter as tk import tkinter.messagebox window = tk.tk() # 实例化tk window.title("my window") # 设置标题 window.geometry("200x200") # 设置窗口的大小 def hit_me(): tk.messagebox.showinfo(title="hi", message="hit") # 注意,要导入模块,还有showwarning, showerror, """ tkinter.messagebox.askokcancel('提示', '要执行此操作吗')#确定/取消,返回值true/false tkinter.messagebox.askquestion('提示', '要执行此操作吗')#是/否,返回值yes/no tkinter.messagebox.askyesno('提示', '要执行此操作吗')#是/否,返回值true/false tkinter.messagebox.askretrycancel('提示', '要执行此操作吗')#重试/取消,返回值true/false 文件对话框 import tkinter.filedialog a = tkinter.filedialog.asksaveasfilename()#返回文件名 print(a) a = tkinter.filedialog.asksaveasfile()#会创建文件 a = tkinter.filedialog.askopenfilename()#返回文件名 a = tkinter.filedialog.askopenfile()#返回文件流对象 a = tkinter.filedialog.askdirectory()#返回目录名 a = tkinter.filedialog.askopenfilenames()#可以返回多个文件名 a = tkinter.filedialog.askopenfiles()#多个文件流对象 tk.button(window, text="hit me", command=hit_me).pack() window.mainloop() # 显示窗口
案例十一
pack grid place 放置
import tkinter as tk window = tk.tk() # 实例化tk window.title("my window") # 设置标题 window.geometry("200x200") # 设置窗口的大小 # tk.label(window, text="pack").pack(side="top") # tk.label(window, text="pack").pack(side="bottom") # tk.label(window, text="pack").pack(side="left") # tk.label(window, text="pack").pack(side="right") # for i in range(4): # 行 # for j in range(3): # 列 # tk.label(window, text="grid").grid(row=i, column=j, padx=10, pady=10) # 以方格的形式放置 tk.label(window, text="place").place(x=100, y=100, anchor="nw") # 点的形式 window.mainloop() # 显示窗口
登录窗口
import tkinter as tk import tkinter.messagebox window = tk.tk() # 实例化tk window.title("login") # 设置标题 window.geometry("450x300") # 设置窗口的大小 user_info = [{"name": "admin", "pwd": "1"}, {"name": "user", "pwd": "1"}] # 存储账号信息 # 定义函数 def login(): user_info_name = [i["name"] for i in user_info] user_info_pwd = [i["pwd"] for i in user_info] user_name = var_user_name.get() user_pwd = var_pwd.get() if user_name in user_info_name: if user_pwd == user_info_pwd[user_info_name.index(user_name)]: tk.messagebox.showinfo(title="欢迎回来", message="how are you") else: tk.messagebox.showerror(message="pwd error") else: answer = tk.messagebox.askquestion(title="warn", message="您还没有账号,是否需要注册?") if answer: sign() def sign(): def store(): new_pwd1 = new_pwd.get() new_user_name1 = new_user_name.get() user_info.append({"name": new_user_name1, "pwd": new_pwd1}) tk.messagebox.showinfo(message="注册成功") window_sign.destroy() # 关闭窗口 def close(): window_sign.destroy() window_sign = tk.toplevel(window) # 窗口上的窗口 window_sign.geometry("350x200") window_sign.title("sign up window") # user tk.label(window_sign, text="name:").place(x=60, y=60) new_user_name = tk.stringvar() # 注意要使用.get()得到值 new_user_name.set("exampe@python.com") # 默认值 tk.entry(window_sign, textvariable=new_user_name).place(x=90, y=60) # 输入用户名 # pwd tk.label(window_sign, text="pwd:").place(x=60, y=100) new_pwd = tk.stringvar() tk.entry(window_sign, textvariable=new_pwd, show="*").place(x=90, y=100) # 输入密码 # 确认按钮 tk.button(window_sign, text="确认", command=store).place(x=100, y=140) # 取消按钮 tk.button(window_sign, text="取消", command=close).place(x=170, y=140) if __name__ == '__main__': tk.label(window, text="user name:").place(x=50, y=50) var_user_name = tk.stringvar() # 注意要使用.get()得到值 var_user_name.set("exampe@python.com") # 默认值 tk.entry(window, textvariable=var_user_name).place(x=130, y=50) # 输入用户名 tk.label(window, text="password:").place(x=50, y=90) var_pwd = tk.stringvar() tk.entry(window, textvariable=var_pwd, show="*").place(x=130, y=90) # 输入密码 # login button tk.button(window, text="login", command=login).place(x=130, y=130) # 登录按钮 # sign button tk.button(window, text="sign up", command=sign).place(x=200, y=130) # 注册按钮 window.mainloop() # 显示窗口
本文来自博客园,作者:a-l-kun,转载请注明原文链接:https://www.cnblogs.com/liuzhongkun/p/15829678.html
到此这篇关于python中的tkinter库简单案例详解的文章就介绍到这了,更多相关python中的tkinter库内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Redis实现分布式锁方法详细
下一篇: MySQL优化(2):存储引擎和锁
推荐阅读
-
python库的tkinter带你进入GUI世界(计算器简单功能)
-
python中mechanize库的简单使用示例
-
对python中的xlsxwriter库简单分析
-
对python3中pathlib库的Path类的使用详解
-
详解Python odoo中嵌入html简单的分页功能
-
使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二)
-
Python 中Pickle库的使用详解
-
python中yield的用法详解——最简单,最清晰的解释
-
用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化
-
SpringSecurity从数据库中获取用户信息进行验证的案例详解