欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中Tkinter復選框Checkbutton是否被選中判斷

 更新時間:2023年01月28日 09:31:36   作者:Gordennizaicunzai  
這篇文章主要介紹了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)值,默認為1
  • offvalue:checkbutton 未被選中時的狀態(tài)值,默認為0
  • variable: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()

在這里插入圖片描述

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論