Python?tkinter?多選按鈕控件?Checkbutton方法
1.多選按鈕的方法
以下為常用的方法:
方法描述deselect()清除多選按鈕選中選項。flash()在激活狀態(tài)顏色和正常顏色之間閃爍幾次多選按鈕,但保持它開始時的狀態(tài)。invoke()可以調(diào)用此方法來獲得與用戶單擊多選按鈕以更改其狀態(tài)時發(fā)生的操作相同的操作select()設置多選按鈕為選中。toggle()選中與沒有選中之間切換
1.2select()
設置某一個多選按鈕為選中的狀態(tài),可以通過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()結果:

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()結果:


1.3 flash()
在激活狀態(tài)顏色和正常顏色之間閃爍幾次多選按鈕,但保持它開始時的狀態(tài)。必須設置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()結果:


1.5 toggle()
切換多選按鈕的狀態(tài)。如果目前是選中的狀態(tài),則變?yōu)槲催x中。反之亦然。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方法的文章就介紹到這了,更多相關Pytho Checkbutton 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python報錯ValueError: cannot reindex from
當處理Pandas數(shù)據(jù)框(DataFrame)時,你是否遇到過ValueError: cannot reindex from a duplicate axis的報錯?這個問題通常發(fā)生在嘗試對DataFrame進行重索引時,如果索引有重復值,就會觸發(fā)這個錯誤,下面,我們將探討這個問題并提供解決方法2024-09-09
Python3實現(xiàn)帶附件的定時發(fā)送郵件功能
這篇文章主要為大家詳細介紹了Python3實現(xiàn)帶附件的定時發(fā)送郵件功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
利用Python實現(xiàn)Windows下的鼠標鍵盤模擬的實例代碼
本篇文章主要介紹了利用Python實現(xiàn)Windows下的鼠標鍵盤模擬的實例代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07
pydantic-resolve嵌套數(shù)據(jù)結構生成LoaderDepend管理contextvars
這篇文章主要為大家介紹了pydantic-resolve解決嵌套數(shù)據(jù)結構生成LoaderDepend管理contextvars的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>2023-04-04

