python中Tkinter復(fù)選框Checkbutton是否被選中判斷
Tkinter復(fù)選框Checkbutton是否被選中判斷
定義一個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非常簡單,所以本文的內(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),整個checkbutton變成灰色狀態(tài)
print(chkbt['variable']) # 輸出 !checkbutton
chkbt['variable'] = 'checkbutton_yudao'
print(chkbt['variable']) # 輸出 checkbutton_yudao
main_win.mainloop()

onvalue:checkbutton 被選中時的狀態(tài)值,默認(rèn)為1offvalue:checkbutton 未被選中時的狀態(tài)值,默認(rèn)為0variable:checkbutton的全局名,默認(rèn)系統(tǒng)會自動給分配,也支持自定義。
常見用法是 記錄checkbutton的選中狀態(tài)值,這個屬性的命名也很有意思,variable,就傳遞了一個信息,variable的值是一個變量,所以,常用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),整個checkbutton變成灰色狀態(tài)
print(chkbt['variable']) # 輸出 !checkbutton
chkbt['variable'] = 'checkbutton_yudao'
print(chkbt['variable']) # 輸出 checkbutton_yudao
main_win.mainloop()
因為沒法截圖,所以自行運(yùn)行后查看效果。
因為是多選框,通過 variable對應(yīng)的變量來判斷對應(yīng)的checkbutton的選中狀態(tài)。
例如,這個實例代碼中,可以通過val和val2來判斷對應(yīng)的checkbutton是否選中,然后在做對應(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')
# 左對齊
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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python數(shù)學(xué)建模PuLP庫線性規(guī)劃實際案例編程詳解
本節(jié)以一個實際數(shù)學(xué)建模案例,來為大家講解PuLP求解線性規(guī)劃問題的建模與編程。來鞏固加深大家對Python數(shù)學(xué)建模PuLP庫線性規(guī)劃的運(yùn)用理解2021-10-10
Python 過濾字符串的技巧,map與itertools.imap
Python中的map函數(shù)非常有用,在字符轉(zhuǎn)換和字符遍歷兩節(jié)都出現(xiàn)過,現(xiàn)在,它又出現(xiàn)了,會給我們帶來什么樣的驚喜呢?是不是要告訴我們,map是非常棒的,以后要多找它玩呢?2008-09-09
Python?異之如何同時運(yùn)行多個協(xié)程詳解
這篇文章主要為大家介紹了Python?異之如何同時運(yùn)行多個協(xié)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

