温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python tkinter多选按钮控件Checkbutton怎么使用

发布时间:2022-08-01 09:26:31 来源:亿速云 阅读:315 作者:iii 栏目:开发技术

这篇文章主要介绍“Python tkinter多选按钮控件Checkbutton怎么使用”,在日常操作中,相信很多人在Python tkinter多选按钮控件Checkbutton怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python tkinter多选按钮控件Checkbutton怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

1.多选按钮的方法

以下为常用的方法:

方法描述deselect()清除多选按钮选中选项。flash()在激活状态颜色和正常颜色之间闪烁几次多选按钮,但保持它开始时的状态。invoke()可以调用此方法来获得与用户单击多选按钮以更改其状态时发生的操作相同的操作select()设置多选按钮为选中。toggle()选中与没有选中之间切换

1.2select()

设置某一个多选按钮为选中的状态,可以通过select()指定特定的单选按钮被选中。

import tkinter as tk root=tk.Tk() root.geometry('300x240') b1 = tk.Checkbutton(root,bg='red',text='红色',bd=5) b1.pack() b2 = tk.Checkbutton(root,text='蓝色',bg='blue',bd=5) b2.pack() b3 = tk.Checkbutton(root,text='绿色',bg='green',bd=5) b3.pack() b2.select() root.mainloop()

结果:

Python tkinter多选按钮控件Checkbutton怎么使用

1.2 deselect()

跟select方法是相反的操作,取消某个单选按钮被选中。

import tkinter as tk root=tk.Tk() root.geometry('300x240') b1 = tk.Checkbutton(root,bg='red',text='红色',bd=5) b1.pack() b2 = tk.Checkbutton(root,text='蓝色',bg='blue',bd=5) b2.pack() b3 = tk.Checkbutton(root,text='绿色',bg='green',bd=5) b3.pack() def deselect():     b2.deselect() b4=tk.Button(root,text='取消蓝色',command=deselect) b4.pack() root.mainloop()

结果:

Python tkinter多选按钮控件Checkbutton怎么使用

Python tkinter多选按钮控件Checkbutton怎么使用

1.3 flash()

在激活状态颜色和正常颜色之间闪烁几次多选按钮,但保持它开始时的状态。必须设置activeforeground或者activebackground中的任何一个或者全部,否则没有效果。注意只有被选中的按钮才会起作用。

import tkinter as tk root=tk.Tk() root.geometry('300x240') check=[tk.StringVar(),tk.StringVar(),tk.StringVar()] for i in range(0,3):     check[i].set("0") b1 = tk.Checkbutton(root,bg='red',text='红色',bd=5,                     variable=check[0],activebackground='green',                     activeforeground='yellow') b1.pack() b2 = tk.Checkbutton(root,text='蓝色',bg='blue',bd=5,                     variable=check[1],activebackground='red',                     activeforeground='yellow') b2.pack() b3 = tk.Checkbutton(root,text='绿色',bg='green',bd=5,                     variable=check[2],activebackground='blue',                     activeforeground='yellow') b3.pack() def flash():     if check[0].get()=="1":         b1.flash()     if check[1].get()=="1":         b2.flash()     if check[2].get()=="1":         b3.flash() b4=tk.Button(root,text='Flash',command=flash) b4.pack() root.mainloop()

1.4 invoke()

模拟多选按钮被选中的情况。

import tkinter as tk root=tk.Tk() root.geometry('300x240') b1 = tk.Checkbutton(root,bg='red',text='红色',bd=5) b1.pack() b2 = tk.Checkbutton(root,text='蓝色',bg='blue',bd=5) b2.pack() b3 = tk.Checkbutton(root,text='绿色',bg='green',bd=5) b3.pack() def invoke():     b2.invoke() b4=tk.Button(root,text='Invoke',command=invoke) b4.pack() root.mainloop()

结果:

Python tkinter多选按钮控件Checkbutton怎么使用

Python tkinter多选按钮控件Checkbutton怎么使用

1.5 toggle()

切换多选按钮的状态。如果目前是选中的状态,则变为未选中。反之亦然。toggle()的效果也invoke()是一样的。

import tkinter as tk root=tk.Tk() root.geometry('300x240') b1 = tk.Checkbutton(root,bg='red',text='红色',bd=5) b1.pack() b2 = tk.Checkbutton(root,text='蓝色',bg='blue',bd=5) b2.pack() b3 = tk.Checkbutton(root,text='绿色',bg='green',bd=5) b3.pack() def toggle():     b2.toggle() b4=tk.Button(root,text='Toggle',command=toggle) b4.pack() root.mainloop()

结果:

Python tkinter多选按钮控件Checkbutton怎么使用

Python tkinter多选按钮控件Checkbutton怎么使用

到此,关于“Python tkinter多选按钮控件Checkbutton怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI