使用方法
R1=tk.Radiobutton(root,text='A',variable=var,value='A',command=printf)
R1.pack()
R2=tk.Radiobutton(root,text='B',variable=var,value='B',command=printf)
R2.pack()
R3=tk.Radiobutton(root,text='C',variable=var,value='C',command=printf)
R3.pack()
该程序创建了三个选项A,B,C;root表示界面,text表示选项的标签,variable表示选择R1时得到的值,value为值,command为函数;
完整程序
import tkinter as tk
root=tk.Tk()
root.title('Radiobutton')
screenwidth=root.winfo_screenwidth()
screenheight=root.winfo_screenheight()
width=500
height=300
x=(screenwidth-width)//2
y=(screenheight-height)//2
root.geometry('%dx%d+%d+%d'%(width,height,x,y))
var=tk.StringVar()
L=tk.Label(root,bg='white',width=20,text=' ')
L.place(x=180,y=50)
def printf():
L.config(text=var.get())
R1=tk.Radiobutton(root,text='A',variable=var,value='A',command=printf)
R1.place(x=230,y=100)
R2=tk.Radiobutton(root,text='B',variable=var,value='B',command=printf)
R2.place(x=230,y=150)
R3=tk.Radiobutton(root,text='C',variable=var,value='C',command=printf)
R3.place(x=230,y=200)
root.mainloop()