TKinter Radiobutton
觉得有用的话,欢迎一起讨论相互学习~Follow Me
感谢莫烦老师
详情
创建windows
window = tk.Tk()
window.title('my window')
window.geometry('500x500')
创建显示label
var1 = tk.StringVar()
l = tk.Label(window, bg='yellow', width=4, textvariable=var1)
l.pack()
循环刷新窗口
window.mainloop() # 不断刷新主窗口
显示函数
def print_selection():
l.config(text='you have selected ' + var.get())
Radiobutton
# 以下意为Radiobutton在window控件上
# 显示为Option A
# 选择时更改变量var的值为A
# 并且会激活print_selection函数
r1 = tk.Radiobutton(window, text='Option A',
variable=var, value='A',
command=print_selection)
r1.pack()
r2 = tk.Radiobutton(window, text='Option B',
variable=var, value='B',
command=print_selection)
r2.pack()
r3 = tk.Radiobutton(window, text='Option C',
variable=var, value='C',
command=print_selection)
r3.pack()
完整代码
# View more python learning tutorial on my Youtube and Youku channel!!!
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
# Youku video tutorial: http://i.youku.com/pythontutorial
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('500x500')
var = tk.StringVar()
l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()
def print_selection():
l.config(text='you have selected ' + var.get())
# 以下意为Radiobutton在window控件上
# 显示为Option A
# 选择时更改变量var的值为A
# 并且会激活print_selection函数
r1 = tk.Radiobutton(window, text='Option A',
variable=var, value='A',
command=print_selection)
r1.pack()
r2 = tk.Radiobutton(window, text='Option B',
variable=var, value='B',
command=print_selection)
r2.pack()
r3 = tk.Radiobutton(window, text='Option C',
variable=var, value='C',
command=print_selection)
r3.pack()
window.mainloop()