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使用微信SDK實現(xiàn)的微信支付功能示例
這篇文章主要介紹了Python使用微信SDK實現(xiàn)的微信支付功能,結(jié)合實例形式分析了Python調(diào)用微信SDK接口實現(xiàn)微信支付功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-06-06簡單了解python的break、continue、pass
這篇文章主要介紹了簡單了解python的break、continue、pass,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07Python計算庫numpy進(jìn)行方差/標(biāo)準(zhǔn)方差/樣本標(biāo)準(zhǔn)方差/協(xié)方差的計算
今天小編就為大家分享一篇關(guān)于Python計算庫numpy進(jìn)行方差/標(biāo)準(zhǔn)方差/樣本標(biāo)準(zhǔn)方差/協(xié)方差的計算,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12Python將字符串常量轉(zhuǎn)化為變量方法總結(jié)
在本篇內(nèi)容里我們給大家整理了一篇關(guān)于Python將字符串常量轉(zhuǎn)化為變量方法的知識點(diǎn)總結(jié),有需要的朋友們學(xué)習(xí)下。2019-03-03把MySQL表結(jié)構(gòu)映射為Python中的對象的教程
這篇文章主要介紹了簡單地把MySQL表結(jié)構(gòu)映射為Python中的對象的方法,用到了Python中的SQLAlchemy庫,需要的朋友可以參考下2015-04-04Python面向?qū)ο蟪绦蛟O(shè)計之繼承與多繼承用法分析
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計之繼承與多繼承用法,結(jié)合實例形式分析了Python繼承與多繼承的簡單定義與使用方法,需要的朋友可以參考下2018-07-07