程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Tkinter - Chapter 9 method of multi selection button control (checkbutton)

編輯:Python

9.2 How to select multiple buttons
The following are common methods :

Method describe deselect() Clear the multiple selection button to select the option .flash() Blink the multi select button several times between the active state color and the normal color , But keep it as it started .invoke() You can call this method to get the same action that happens when a user clicks a multi select button to change its state select() Set the multi selection button to selected .toggle() Switch between checked and unchecked

9.2.1 select()
Set a multi selection button to the selected status , Can pass select() Specifies that a specific radio button is selected .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5)
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5)
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5)
b3.pack()
b2.select()
root.mainloop()

result :

9.2.2 deselect()
Follow select Method is the opposite operation , Cancel a radio button is selected .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5)
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5)
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5)
b3.pack()
def deselect():
b2.deselect()
b4=tk.Button(root,text=' Cancel blue ',command=deselect)
b4.pack()
root.mainloop()

result :


9.2.3 flash()
Blink the multi select button several times between the active state color and the normal color , But keep it as it started . You have to set activeforeground perhaps activebackground Any or all of , Otherwise it won't work . Note that only the selected button will work .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
check=[tk.StringVar(),tk.StringVar(),tk.StringVar()]
for i in range(0,3):
check[i].set("0")
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5,
variable=check[0],activebackground='green',
activeforeground='yellow')
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5,
variable=check[1],activebackground='red',
activeforeground='yellow')
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5,
variable=check[2],activebackground='blue',
activeforeground='yellow')
b3.pack()
def flash():
if check[0].get()=="1":
b1.flash()
if check[1].get()=="1":
b2.flash()
if check[2].get()=="1":
b3.flash()
b4=tk.Button(root,text='Flash',command=flash)
b4.pack()
root.mainloop()

9.2.4 invoke()
Simulate the situation when the multi selection button is selected .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5)
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5)
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5)
b3.pack()
def invoke():
b2.invoke()
b4=tk.Button(root,text='Invoke',command=invoke)
b4.pack()
root.mainloop()

result :


9.2.5 toggle()
Switch the status of multiple selection buttons . If it is currently selected , It becomes unselected . vice versa .toggle() The effect of invoke() It's the same .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5)
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5)
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5)
b3.pack()
def toggle():
b2.toggle()
b4=tk.Button(root,text='Toggle',command=toggle)
b4.pack()
root.mainloop()

result :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved