python中Tkinter復選框Checkbutton是否被選中判斷
Tkinter復選框Checkbutton是否被選中判斷
定義一個BooleanVar型數(shù)據(jù)進行獲取復選框狀態(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非常簡單,所以本文的內容也非常的輕松,讓我們開始吧!
checkbutton
:checkbutton也就是我們常說的復選框。text
:設置checkbutton顯示的文字bg
:設置背景顏色fg
:設置前景顏色bd
:設置checkbutton的邊框寬度relief
:設置顯示樣式underline
:設置顯示的文字是否帶下劃線state
:checkbutton是否響應用戶操作, 值為’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設置為 不可操作狀態(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)值,默認為1offvalue
:checkbutton 未被選中時的狀態(tài)值,默認為0variable
:checkbutton的全局名,默認系統(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設置為 不可操作狀態(tài),整個checkbutton變成灰色狀態(tài) print(chkbt['variable']) # 輸出 !checkbutton chkbt['variable'] = 'checkbutton_yudao' print(chkbt['variable']) # 輸出 checkbutton_yudao main_win.mainloop()
因為沒法截圖,所以自行運行后查看效果。
因為是多選框,通過 variable對應的變量來判斷對應的checkbutton的選中狀態(tài)。
例如,這個實例代碼中,可以通過val和val2來判斷對應的checkbutton是否選中,然后在做對應的處理。
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設置為選中狀態(tài) lan_c_plus_plus.deselect() # 將lan_c_plus_plus設置為未選中狀態(tài) lan_c_plus_plus.toggle() # 切換lan_c_plus_plus的狀態(tài) main_win.mainloop()
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python使用微信SDK實現(xiàn)的微信支付功能示例
這篇文章主要介紹了Python使用微信SDK實現(xiàn)的微信支付功能,結合實例形式分析了Python調用微信SDK接口實現(xiàn)微信支付功能的具體步驟與相關操作技巧,需要的朋友可以參考下2017-06-06簡單了解python的break、continue、pass
這篇文章主要介紹了簡單了解python的break、continue、pass,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07Python計算庫numpy進行方差/標準方差/樣本標準方差/協(xié)方差的計算
今天小編就為大家分享一篇關于Python計算庫numpy進行方差/標準方差/樣本標準方差/協(xié)方差的計算,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12