Tkinter(三)——Radiobutton选择控件

# Radio button

import tkinter as tk

window = tk.Tk() #对象,object
window.title('my window')
window.geometry('500x300')


l = tk.Label(window,bg = 'yellow',width = 20)
l.pack()

var = tk.StringVar() # 也是一个对象,字符串对象

def print_selection():
    l.config(text = 'you have selected' + ' ' + var.get())

# button1  
r1 = tk.Radiobutton(window,text = 'Option A',
                    variable = var,
                    value = 'A',
                    command = print_selection)
r1.pack()

# button2
r2 = tk.Radiobutton(window,text = 'Option B',
                    variable = var,
                    value = 'B',
                    command = print_selection)
r2.pack()

# button 3
r3 = tk.Radiobutton(window,text = 'Option C',
                    variable = var,
                    value = 'C',
                    command = print_selection)
r3.pack()

猜你喜欢

转载自blog.csdn.net/qq_39326657/article/details/81671300