#encoding=utf-8
import tkinter as tk
window = tk.Tk()
window.title("測試單選框")
window.geometry("400x200")
var = tk.StringVar()
label_1 = tk.Label(window,bg="yellow",textvar=var,width=10,height=1,text="label_1")
label_1.pack()
def print_selection():
#label的config函數(shù)可以改變label對象里定義的參數(shù)的值
label_1.config(text= "你選擇了選項: " + var.get())
label_1.config(bg="green")
label_1.config(width=12)
#定義單選按鈕,var=var,value="A" 指把value的值賦值給var
radiobutton_1 = tk.Radiobutton(window,text="選項A",var=var,value="A",command=print_selection)
radiobutton_1.pack()
radiobutton_2 = tk.Radiobutton(window,text="選項B",var=var,value="B",command=print_selection)
radiobutton_2.pack()
radiobutton_3 = tk.Radiobutton(window,text="選項C",var=var,value="C",command=print_selection)
radiobutton_3.pack()
window.mainloop()
網(wǎng)頁題目:tkinter學(xué)習(xí)_radiobutton
文章來源:
http://weahome.cn/article/gjddps.html