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


1.3 flash()
在激活狀態(tài)顏色和正常顏色之間閃爍幾次多選按鈕,但保持它開(kāi)始時(shí)的狀態(tài)。必須設(shè)置activeforeground或者activebackground中的任何一個(gè)或者全部,否則沒(méi)有效果。注意只有被選中的按鈕才會(huì)起作用。
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='藍(lán)色',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='藍(lán)色',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='藍(lán)色',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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python報(bào)錯(cuò)ValueError: cannot reindex from
當(dāng)處理Pandas數(shù)據(jù)框(DataFrame)時(shí),你是否遇到過(guò)ValueError: cannot reindex from a duplicate axis的報(bào)錯(cuò)?這個(gè)問(wèn)題通常發(fā)生在嘗試對(duì)DataFrame進(jìn)行重索引時(shí),如果索引有重復(fù)值,就會(huì)觸發(fā)這個(gè)錯(cuò)誤,下面,我們將探討這個(gè)問(wèn)題并提供解決方法2024-09-09
Python3實(shí)現(xiàn)帶附件的定時(shí)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了Python3實(shí)現(xiàn)帶附件的定時(shí)發(fā)送郵件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
使用Python編寫(xiě)簡(jiǎn)單的端口掃描器的實(shí)例分享
這篇文章主要介紹了使用Python編寫(xiě)簡(jiǎn)單的端口掃描器的實(shí)例分享,文中分別介紹了單線程和多線程的實(shí)現(xiàn)方式,需要的朋友可以參考下2015-12-12
解決python 無(wú)法加載downsample模型的問(wèn)題
今天小編就為大家分享一篇解決python 無(wú)法加載downsample模型的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
python動(dòng)態(tài)加載技術(shù)解析
這篇文章主要介紹了python動(dòng)態(tài)加載技術(shù)解析,說(shuō)簡(jiǎn)單點(diǎn)就是,如果開(kāi)發(fā)者發(fā)現(xiàn)自己的代碼有bug,那么他可以在不關(guān)閉原來(lái)代碼的基礎(chǔ)之上,動(dòng)態(tài)替換模塊替換方法一般用reload來(lái)完成,需要的朋友可以參考下2023-07-07
利用Python實(shí)現(xiàn)Windows下的鼠標(biāo)鍵盤(pán)模擬的實(shí)例代碼
本篇文章主要介紹了利用Python實(shí)現(xiàn)Windows下的鼠標(biāo)鍵盤(pán)模擬的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
python中取絕對(duì)值簡(jiǎn)單方法總結(jié)
在本篇內(nèi)容里小編給大家整理的是關(guān)于python中取絕對(duì)值簡(jiǎn)單方法,需要的朋友們可以學(xué)習(xí)下。2020-07-07
pydantic-resolve嵌套數(shù)據(jù)結(jié)構(gòu)生成LoaderDepend管理contextvars
這篇文章主要為大家介紹了pydantic-resolve解決嵌套數(shù)據(jù)結(jié)構(gòu)生成LoaderDepend管理contextvars的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-04-04

