使用方法
var1=tk.IntVar()
var2=tk.IntVar()
def printf():
if (var1.get()==1)&(var2.get()==0):
L.config(text='1')
elif (var1.get()==0)&(var2.get()==1):
L.config(text='2')
elif (var1.get()==0)&(var2.get()==0):
L.config(text='None')
else:
L.config(text='Both')
C1=tk.Checkbutton(root,text='A',variable=var1,onvalue=1,offvalue=0,command=printf)
C1.place(x=230,y=100)
C2=tk.Checkbutton(root,text='B',variable=var2,onvalue=1,offvalue=0,command=printf)
C2.place(x=230,y=150)
创建两个选项,当选择第一个选项A时,打印1,选择第二个选项B时,打印2,两个都选打印'Both',两个都不选打印'None'
完整程序
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)
var1=tk.IntVar()
var2=tk.IntVar()
def printf():
if (var1.get()==1)&(var2.get()==0):
L.config(text='1')
elif (var1.get()==0)&(var2.get()==1):
L.config(text='2')
elif (var1.get()==0)&(var2.get()==0):
L.config(text='None')
else:
L.config(text='Both')
C1=tk.Checkbutton(root,text='A',variable=var1,onvalue=1,offvalue=0,command=printf)
C1.place(x=230,y=100)
C2=tk.Checkbutton(root,text='B',variable=var2,onvalue=1,offvalue=0,command=printf)
C2.place(x=230,y=150)
L.mainloop()