Python| Tkinter ttk.Checkbutton 和简单的 Checkbutton 比较
Tkinter 是一个与Python本身一起提供的 GUI(图形用户界面)模块。该模块广泛用于创建 GUI 应用程序。 tkinter.ttk
用于创建具有现代图形效果的 GUI 应用程序,这是仅使用tkinter无法实现的。复选按钮用于选择多个选项。
可以使用以下代码段创建检查按钮。
chkbtn = ttk.Checkbutton(master, option=value, ...)
代码#1:
# importing tkinter.ttk
from tkinter import *
from tkinter.ttk import *
# creating root
root = Tk()
# label text
Label(root, text ='Select Programming language of your choice').place(x = 20, y = 0)
# check buttons
java = Checkbutton(root, text ='Java',
takefocus = 0).place(x = 40, y = 30)
cpp = Checkbutton(root, text ='C++',
takefocus = 0).place(x = 40, y = 50)
python = Checkbutton(root, text ='Python',
takefocus = 0).place(x = 40, y = 70)
c = Checkbutton(root, text ='C',
takefocus = 0).place(x = 40, y = 90)
root.mainloop()
输出:
代码 #2:简单Checkbutton
和ttk.Checkbutton
之间的区别
# importing tkinter and ttk
from tkinter import *
from tkinter import ttk
# root
root = Tk()
# This will depict the features of Simple Checkbutton
Label(root, text ='Simple Checkbutton').place(x = 10, y = 10)
chkbtn1 = Checkbutton(root, text ='Checkbutton1',
takefocus = 0).place(x = 10, y = 40)
chkbtn2 = Checkbutton(root, text ='Checkbutton2',
takefocus = 0).place(x = 10, y = 60)
# This will depict the features of ttk.Checkbutton
Label(root, text ='ttk.Checkbutton').place(x = 140, y = 10)
chkbtn1 = ttk.Checkbutton(root, text ='Checkbutton1',
takefocus = 0).place(x = 140, y = 40)
chkbtn2 = ttk.Checkbutton(root, text ='Checkbutton2',
takefocus = 0).place(x = 140, y = 60)
root.mainloop()
输出:
请注意两个 Checkbuttons 的外观差异,这都是由于现代图形。在上面的代码中,当鼠标悬停在ttk.Checkbutton
上时,您可能会看到蓝色效果(效果可能会从 os 变为 os)。当我们将鼠标悬停在 Simple Checkbutton 上时,您可能不会遇到任何此类效果。