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

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


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


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


到此這篇關(guān)于Python tkinter 多選按鈕控件 Checkbutton方法的文章就介紹到這了,更多相關(guān)Pytho Checkbutton 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用python實現(xiàn)mqtt的發(fā)布和訂閱
這篇文章主要介紹了使用python實現(xiàn)mqtt的發(fā)布和訂閱,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
Python Django網(wǎng)頁界面協(xié)同過濾推薦算法實現(xiàn)商品管理與推薦
商品管理與推薦系統(tǒng),本系統(tǒng)使用Python作為主要開發(fā)語言,前端采用HTML、CSS、BootStrap等技術(shù)搭建顯示界面,后端采用Django框架處理用戶的請求響應(yīng)2023-11-11
Python實現(xiàn)從log日志中提取ip的方法【正則提取】
這篇文章主要介紹了Python實現(xiàn)從log日志中提取ip的方法,涉及Python文件讀取、數(shù)據(jù)遍歷、正則匹配等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
pytorch 預(yù)訓(xùn)練模型讀取修改相關(guān)參數(shù)的填坑問題
這篇文章主要介紹了pytorch 預(yù)訓(xùn)練模型讀取修改相關(guān)參數(shù)的填坑問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
win10系統(tǒng)配置GPU版本Pytorch的詳細教程
這篇文章主要介紹了win10系統(tǒng)配置GPU版本Pytorch,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

