python中Tkinter復(fù)選框Checkbutton是否被選中判斷
Tkinter復(fù)選框Checkbutton是否被選中判斷
定義一個(gè)BooleanVar型數(shù)據(jù)進(jìn)行獲取復(fù)選框狀態(tài)。
>>> import tkinter as tk >>> >>> window = tk.Tk() >>> var = tk.BooleanVar() >>> def get_var(): print(var.get()) >>> cb = tk.Checkbutton(window, text="debug", variable=var, command=get_var) >>> cb.pack() >>> window.mainloop() True False True False True

tkinter-checkbutton詳解
介紹checkbutton的使用,由于checkbutton非常簡(jiǎn)單,所以本文的內(nèi)容也非常的輕松,讓我們開始吧!
checkbutton:checkbutton也就是我們常說的復(fù)選框。text:設(shè)置checkbutton顯示的文字bg:設(shè)置背景顏色fg:設(shè)置前景顏色bd:設(shè)置checkbutton的邊框?qū)挾?/li>relief:設(shè)置顯示樣式underline:設(shè)置顯示的文字是否帶下劃線state:checkbutton是否響應(yīng)用戶操作, 值為’normal’,‘active’,‘disabled’
from tkinter import Tk,Checkbutton
main_win = Tk()
main_win.title('漁道的checkbutton控件')
width = 300
height = 300
main_win.geometry(f'{width}x{height}')
chkbt = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=10, relief='raised', underline=0, command=test_cb)
chkbt2 = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=5, relief='raised')
chkbt.pack()
chkbt2.pack()
print(chkbt['state']) # 輸出 normal
chkbt['state'] = 'disabled' # 將chkbt設(shè)置為 不可操作狀態(tài),整個(gè)checkbutton變成灰色狀態(tài)
print(chkbt['variable']) # 輸出 !checkbutton
chkbt['variable'] = 'checkbutton_yudao'
print(chkbt['variable']) # 輸出 checkbutton_yudao
main_win.mainloop()

onvalue:checkbutton 被選中時(shí)的狀態(tài)值,默認(rèn)為1offvalue:checkbutton 未被選中時(shí)的狀態(tài)值,默認(rèn)為0variable:checkbutton的全局名,默認(rèn)系統(tǒng)會(huì)自動(dòng)給分配,也支持自定義。
常見用法是 記錄checkbutton的選中狀態(tài)值,這個(gè)屬性的命名也很有意思,variable,就傳遞了一個(gè)信息,variable的值是一個(gè)變量,所以,常用IntVar作為variable屬性的值。
from tkinter import Tk,Checkbutton
main_win = Tk()
main_win.title('漁道的checkbutton控件')
width = 300
height = 300
main_win.geometry(f'{width}x{height}')
chkbt = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=10, relief='raised', underline=0, command=test_cb)
chkbt2 = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=5, relief='raised')
chkbt.pack()
chkbt2.pack()
print(chkbt['state']) # 輸出 normal
chkbt['state'] = 'disabled' # 將chkbt設(shè)置為 不可操作狀態(tài),整個(gè)checkbutton變成灰色狀態(tài)
print(chkbt['variable']) # 輸出 !checkbutton
chkbt['variable'] = 'checkbutton_yudao'
print(chkbt['variable']) # 輸出 checkbutton_yudao
main_win.mainloop()
因?yàn)闆]法截圖,所以自行運(yùn)行后查看效果。
因?yàn)槭嵌噙x框,通過 variable對(duì)應(yīng)的變量來(lái)判斷對(duì)應(yīng)的checkbutton的選中狀態(tài)。
例如,這個(gè)實(shí)例代碼中,可以通過val和val2來(lái)判斷對(duì)應(yīng)的checkbutton是否選中,然后在做對(duì)應(yīng)的處理。
select():使checkbutton處于選中狀態(tài)(on-state)deselect():使checkbutton處于選中未狀態(tài)(off-state)toggle():切換checkbutton的選中狀態(tài)
from tkinter import Tk,Checkbutton
main_win = Tk()
main_win.title('漁道的checkbutton控件')
width = 300
height = 300
main_win.geometry(f'{width}x{height}')
def test_cb():
print(lan_c['state'])
print(lan_c['variable'])
print(lan_c['tristatevalue'])
print(lan_c['onvalue'])
print(lan_c['offvalue'])
lan_python = Checkbutton(main_win, text='python', bg='yellow')
lan_c = Checkbutton(main_win, text='c', bg='blue', command=test_cb, relief='raised', bd=5)
lan_c_plus_plus = Checkbutton(main_win, text='c++', bg='yellow', underline=0)
lan_java = Checkbutton(main_win, text='java', bg='blue')
lan_php = Checkbutton(main_win, text='php', bg='yellow')
lan_html5 = Checkbutton(main_win, text='html5', bg='blue')
lan_js = Checkbutton(main_win, text='javascript', bg='yellow')
# 左對(duì)齊
lan_python.pack(anchor='w')
lan_c.pack(anchor='w')
lan_c_plus_plus.pack(anchor='w')
lan_java.pack(anchor='w')
lan_php.pack(anchor='w')
lan_html5.pack(anchor='w')
lan_js.pack(anchor='w')
lan_c_plus_plus.select() # 將lan_c_plus_plus設(shè)置為選中狀態(tài)
lan_c_plus_plus.deselect() # 將lan_c_plus_plus設(shè)置為未選中狀態(tài)
lan_c_plus_plus.toggle() # 切換lan_c_plus_plus的狀態(tài)
main_win.mainloop()

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python數(shù)學(xué)建模PuLP庫(kù)線性規(guī)劃實(shí)際案例編程詳解
本節(jié)以一個(gè)實(shí)際數(shù)學(xué)建模案例,來(lái)為大家講解PuLP求解線性規(guī)劃問題的建模與編程。來(lái)鞏固加深大家對(duì)Python數(shù)學(xué)建模PuLP庫(kù)線性規(guī)劃的運(yùn)用理解2021-10-10
PyTorch中torch.nn模塊的實(shí)現(xiàn)
torch.nn是PyTorch中用于構(gòu)建神經(jīng)網(wǎng)絡(luò)的核心模塊,包括多種組件,每個(gè)組件都有其特定的原理和使用場(chǎng)景,本文就來(lái)詳細(xì)的介紹一下如何使用,感興趣的可以了解一下2024-09-09
Python 過濾字符串的技巧,map與itertools.imap
Python中的map函數(shù)非常有用,在字符轉(zhuǎn)換和字符遍歷兩節(jié)都出現(xiàn)過,現(xiàn)在,它又出現(xiàn)了,會(huì)給我們帶來(lái)什么樣的驚喜呢?是不是要告訴我們,map是非常棒的,以后要多找它玩呢?2008-09-09
python通過第三方庫(kù)操作PDF文件的幾種常見方法
Python是一種高級(jí)編程語(yǔ)言,主要用于數(shù)據(jù)分析、機(jī)器學(xué)習(xí)、圖像處理等領(lǐng)域,在PDF文件處理方面,Python有許多強(qiáng)大的庫(kù)和工具,這篇文章主要給大家介紹了關(guān)于python通過第三方庫(kù)操作PDF文件的幾種常見方法,需要的朋友可以參考下2024-02-02
Python?異之如何同時(shí)運(yùn)行多個(gè)協(xié)程詳解
這篇文章主要為大家介紹了Python?異之如何同時(shí)運(yùn)行多個(gè)協(xié)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

