我想根据选中的单选按钮打印不同的文本。我的if语句应如何显示,以便我可以看到检查了哪个按钮?
from tkinter import *
from tkinter import ttk
myApp = Tk()
myApp.title("Cost Analysis")
RadioPlanned=Radiobutton(predictionframe1, text="option 1", value=1)
RadioPlanned.grid(row=0,column=0)
RadioReal=Radiobutton(predictionframe1, text="option 2", value=2)
RadioReal.grid(row=1,column=0)
def printing():
if # radio button "option 1 " is checked
print("option 1")
elif # radio button "option 2 " is checked
print("option 2")
but=Button(myApp, text="button", command=printing)
but.grid(row=2, column=0)
myApp.mainloop()
答案 0 :(得分:1)
我找到了解决方案,这里是代码:
from tkinter import *
from tkinter import ttk
myApp = Tk()
myApp.title("Cost Analysis")
option=IntVar()
RadioPlanned=Radiobutton(myApp, text="option 1", value=1, variable=option)
RadioPlanned.grid(row=0,column=0)
RadioReal=Radiobutton(myApp, text="option 2", value=2, variable=option)
RadioReal.grid(row=1,column=0)
def printing():
if option.get()==1:
print("option 1")
elif option.get()==2:
print("option 2")
but=Button(myApp, text="button", command=printing)
but.grid(row=2, column=0)
myApp.mainloop()