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

Python Tkinter - Chapter 8 radio button control (RadioButton) method

編輯:Python

8.2 Radio button control method
The following are common methods :

Method describe deselect() Clear the radio button to select the option .flash() Blink the radio button several times between the active 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 radio button to change its state select() Set button to selected .8.2.1 select() Set a radio button to the selected status . such as , By default, the first defined radio button is selected , We can go through select() Specifies that a specific radio button is selected .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
color=tk.StringVar()
color.set('red')
b1 = tk.Radiobutton(root,bg='red',text=' Red ',
variable=color,value='red')
b1.pack()
b2 = tk.Radiobutton(root,text=' Blue ',bg='blue',
variable=color,value='blue')
b2.pack()
b3 = tk.Radiobutton(root,text=' green ',bg='green',
variable=color,value='green')
b3.pack()
b2.select()
root.mainloop()

result :

8.2.2 deselect()
Clear the radio button selection . For example, at the beginning , yes ’ Red ’ The radio button for is selected , have access to deselect() Method , Cancel the selection . This is the time , No radio buttons are selected

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
color=tk.StringVar()
color.set('red')
b1 = tk.Radiobutton(root,bg='red',text=' Red ',
variable=color,value='red')
b1.pack()
b2 = tk.Radiobutton(root,text=' Blue ',bg='blue',
variable=color,value='blue')
b2.pack()
b3 = tk.Radiobutton(root,text=' green ',bg='green',
variable=color,value='green')
b3.pack()
b1.deselect()
root.mainloop()

result :

explain ; At this time 3 A radio button has no value selected , So it's a state of uncertainty . here color The value of the variable is empty , Although it is set at the beginning of the program color The value of is ‘ Red ’, because deselect() Method ,color The value of is cleared .
8.2.3 flash()
Blink the radio button several times between the active 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 .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
color=tk.StringVar()
color.set('red')
b1 = tk.Radiobutton(root,bg='red',text=' Red ',activebackground='green',
activeforeground='yellow',variable=color,value='red')
b1.pack()
b2 = tk.Radiobutton(root,text=' Blue ',bg='blue',activebackground='red',
activeforeground='yellow',variable=color,value='blue')
b2.pack()
b3 = tk.Radiobutton(root,text=' green ',bg='green',activebackground='blue',
activeforeground='yellow',variable=color,value='green')
b3.pack()
def flash():
c=color.get()
if c== 'red':
b1.flash()
elif c=='blue':
b2.flash()
elif c=='green':
b3.flash()
else:
return
b5 = tk.Button(root,text='Flash',command=flash)
b5.pack()
root.mainloop()

8.2.4 invoke()
Simulate clicking the radio button . Call this function , You can select the corresponding radio button .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
color=tk.StringVar()
color.set('red')
b1 = tk.Radiobutton(root,bg='red',text=' Red ',
variable=color,value='red')
b1.pack()
b2 = tk.Radiobutton(root,text=' Blue ',bg='blue',
variable=color,value='blue')
b2.pack()
b3 = tk.Radiobutton(root,text=' green ',bg='green',
variable=color,value='green')
b3.pack()
def invoke():
b3.invoke()
b5 = tk.Button(root,text='Invoke',command=invoke)
b5.pack()
root.mainloop()

result :


explain : The red button is selected at the beginning . Click on ’Invoke’ After button , system call invoke() Method , The set green button is selected .


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