网络编程
位置:首页>> 网络编程>> Python编程>> Python tkinter 多选按钮控件 Checkbutton方法

Python tkinter 多选按钮控件 Checkbutton方法

作者:hqx  发布时间:2022-12-31 08:38:46 

标签: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方法

来源:https://blog.csdn.net/weixin_42272768/article/details/100725162

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com