脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python tkinter 多选按钮控件 Checkbutton方法

Python tkinter 多选按钮控件 Checkbutton方法

2022-07-31 23:21hqx Python

这篇文章主要介绍了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方法的文章就介绍到这了,更多相关Pytho Checkbutton 内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/weixin_42272768/article/details/100725162

延伸 · 阅读

精彩推荐
  • Python详解js文件通过python访问数据库方法

    详解js文件通过python访问数据库方法

    在本篇内容里小编给大家分享了关于js文件通过python访问数据库方法和技巧,有需要的朋友们跟着学习参考下。...

    脚本之家4062021-06-04
  • Pythonpython之json文件转xml文件案例讲解

    python之json文件转xml文件案例讲解

    这篇文章主要介绍了python之json文件转xml文件案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    G果5142021-12-17
  • Python对python mayavi三维绘图的实现详解

    对python mayavi三维绘图的实现详解

    今天小编就为大家分享一篇对python mayavi三维绘图的实现详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    落叶_小唱11872021-05-12
  • Pythonpython实现多图像叠置输出

    python实现多图像叠置输出

    这篇文章主要为大家详细介绍了python实现多图像叠置输出,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    劲酒奶奶11832021-10-03
  • Pythonpython-docx 页面设置详解

    python-docx 页面设置详解

    今天小编就为大家分享一篇python docx 中页面的设置,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    站着活4032022-01-21
  • Python学习Python中一些实用的库

    学习Python中一些实用的库

    这篇文章主要介绍了Python学习之盘点一些Python中实用的库,有需要的同学可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    CXYhh1216892022-01-10
  • PythonPython3 处理JSON的实例详解

    Python3 处理JSON的实例详解

    这篇文章主要介绍了Python3 处理JSON的实例详解的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下...

    dove19806002020-12-14
  • Pythonpython人物视频背景替换实现虚拟空间穿梭

    python人物视频背景替换实现虚拟空间穿梭

    这篇文章主要为大家介绍了python实现人物视频背景替换示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    小小杨树3932022-07-29