我想根据选中什么单选按钮打印不同的文本。如果语句看起来应该如何看,以便我可以查看哪个按钮?
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()
我找到了解决方案,这是代码:
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()