python tkinter知识点使用记录

python tkinter知识点使用记录,第1张

概述引入模块与创建实例importtkinterastkroot=tk.TK()窗口属性设置#设置窗口标题root.title('考试广播系统')#设置窗口图标root.iconbitmap(EXAM_ICON)#设置窗口背景色root.configure(background='#d4d0c8')#禁用调整GUI大小root.resizable(0,0)#获取屏 引入模块与创建实例
import tkinter as tkroot = tk.TK()
窗口属性设置
# 设置窗口标题root.Title('考试广播系统')# 设置窗口图标root.iconbitmap(EXAM_ICON)# 设置窗口背景色root.configure(background='#d4d0c8')# 禁用调整GUI大小root.resizable(0, 0)# 获取屏幕宽高sc_w = self.winfo_screenwIDth()sc_h = self.winfo_screenheight()# 设置窗口大小root.geometry(f"560x360+{(sc_w - 560) // 2}+{(sc_h - 360) // 2 - 40}")# 隐藏窗口,设置后窗口固定root.overrIDeredirect(1)
退出时d窗确定
from tkinter import messageBoxdef quit_ui():	if messageBox.askokcancel("退出", "你想退出窗口吗?"):	   self.quit()	   self.destroy()# 设置窗口属性时一并设置root.protocol('WM_DELETE_WINDOW', quit_ui)
如何向绑定方法中传递参数

一般向按钮添加事件方法:

tk.button(self, text='登录(L)', wIDth=10, bg="#d4d0c8", command=login_btn).place(x=164, y=300)

如果向在绑定方法时传递参数,可以使用下面这种方法:

tk.button(self, text='登录(L)', wIDth=10, bg="#d4d0c8", command=lambda: login_btn('l')).place(x=164, y=300)
按钮绑定快捷键
# 在方法中定义触发事件root.bind_all('<Control-l>', login_btn)
鼠标放在按钮提示信息
import Pmwballoon = Pmw.Balloon(root)# 创建按钮对象quit_btn = tk.button(self, image=take_quit_img, bg="#d4d0c8",             		 command=lambda: _audio_control("quit"))quit_btn.place(wIDth=30, height=130, x=870, y=382)balloon.bind(quit_btn, "隐藏控件")

注意:在创建控件后,如何先布局再赋值,那么控件对象是无效,需要先赋值再布局

# 这种方式是无法实现赋值的quit_btn = tk.button(self, image=take_quit_img, bg="#d4d0c8",             		 command=lambda: _audio_control("quit")).place(wIDth=30, height=130, x=870, y=382)
输入框接收数据

注意:如果是在类中创建tk对象,那么tk.StringVar()需要在__init__方法中声明,不然不能使用

fwq_var_name = tk.StringVar()# 输入框设置初始值fwq_var_name.set("七星耀月")tk.Entry(root, textvariable=fwq_var_name, wIDth=38, bd=3).place(x=250, y=100)
如何显示图片
# 比如为按钮控件添加图片take_ws_img = tk.PhotoImage('图片所在绝对路径')sshow_btn = tk.button(root, image=take_ws_img , bg="#d4d0c8", command=show_other_btn)show_btn.place(wIDth=50, height=50, x=813, y=13)
动态更改控件的属性
# 比如动态更改按钮显示的图片,在config中修改指定参数即可show_btn.config(image=take_right_img)
实现下拉菜单
km_var_name = tk.StringVar()  # 接收下拉选择的值SUBJECT_List = ("语文", "数学", "英语", "物理", "化学", "地理", "历史")sub_Box = ttk.ComboBox(root, textvariable=km_var_name)sub_Box["values"] = SUBJECT_Listsub_Box.current(0)sub_Box.bind("<<ComboBoxSelected>>", get_sub_Box)sub_Box.place(wIDth=150, height=24, x=175, y=103)

效果参考:

实现切换导航栏
btn_choose_value = tk.Intvar()btn_choose_value.set(0)tk.Radiobutton(root, variable=self.btn_choose_value, bg="#d4d0c8", anchor="n", text="信息提示", value=0, indicatoron=0, command=self.show_or_hIDe_info).place(x=380, y=336)tk.Radiobutton(root, variable=self.btn_choose_value, bg="#d4d0c8", text="语音播放内容", alue=1, anchor="n", indicatoron=0, command=self.show_or_hIDe_info).place(x=488, y=336)

效果参考:

实现列表
menu_frame = tk.Frame()frame = tk.LabelFrame(root, labelWidget=menu_frame, bg="white", borderwIDth=2, padx=10, pady=8, relIEf="sunken")y_bar = tk.Scrollbar(frame, orIEnt="vertical", bd=0, wIDth=14)List_Box = tk.ListBox(frame, bg="white", yscrollcommand=y_bar.set, border=0, highlightthickness=0, selectforeground="blue", selectbackground="#d4d0c8", active, Font=("微软雅黑", 8), height=4)y_bar['command'] = List_Box.yvIEwy_bar.pack(sIDe="right", fill="y")List_Box.pack_forget()info_List_Box = tk.ListBox(frame, bg="white", yscrollcommand=y_bar.set, border=0, highlightthickness=0, Font=("微软雅黑", 8), height=4)y_bar['command'] = info_List_Box.yvIEwy_bar.pack(sIDe="right", fill="y")info_List_Box.pack(anchor="nw", fill="both", expand="yes")info_List_Box.insert('end', f"[{time.strftime('%Y-%m-%d %H:%M:%s')}] 启动服务")info_List_Box.insert('end', f"[{time.strftime('%Y-%m-%d %H:%M:%s')}] 更新语音文件成功")frame.place(wIDth=480, height=124, x=380, y=362)

效果参考:

推荐参考文档

大力推荐:http://www.bczl.xyz/tkinter/doc/

参考内容比较少:菜鸟教程

总结

以上是内存溢出为你收集整理的python tkinter知识点使用记录全部内容,希望文章能够帮你解决python tkinter知识点使用记录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1189117.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存