【发布时间】:2021-06-09 14:20:33
【问题描述】:
在我的代码中,我不明白单选按钮是如何工作的。我的问题是,单选按钮变量没有进一步传递。
from tkinter import *
class SimpleDialog(Frame):
def __init__(self):
super().__init__()
self.output1 = ""
self.output10 = ""
self.output11 = ""
self.output12 = ""
self.initUI()
def initUI(self):
self.master.title("New Input")
self.pack(fill=BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=X)
lbl1 = Label(frame1, text="Name:", width=20)
lbl1.pack(side=LEFT, padx=5, pady=10)
self.entry1 = Entry(frame1, textvariable=self.output1)
self.entry1.insert(0,"Your name")
self.entry1.pack(fill=X, padx=5, expand=True)
auswahl = IntVar()
auswahl.set(0)
frame10 = Frame(self)
frame10.pack(fill=X)
lbl10 = Label(frame10, text="Do you like tomatoes?", width=40)
lbl10.pack(side=LEFT, padx=5, pady=5)
self.entry10 = Radiobutton(frame10, text="No",variable=auswahl, padx = 0, value=0).pack(anchor=W)
self.entry11 = Radiobutton(frame10, text="Sometimes",variable=auswahl,padx = 0, value=1).pack(anchor=W)
self.entry12 = Radiobutton(frame10, text="Yes",variable=auswahl,padx = 0, value=2).pack(anchor=W)
frame3 = Frame(self)
frame3.pack(fill=X)
print(self.entry12)
btn = Button(frame3, text="New entry", command=self.onSubmit)
btn.pack(padx=5, pady=10)
def onSubmit(self):
# Input
self.output1 = self.entry1.get()
# Radio-Buttons
self.output10 = self.entry10
self.output11 = self.entry11
self.output12 = self.entry12
self.quit()
def main():
# This part triggers the dialog
root = Tk()
root.geometry("650x300+500+300")
app = SimpleDialog()
root.mainloop()
# Here we can act on the form components or
# better yet, copy the output to a new variable
user_input = (app.output1, app.output10, app.output11, app.output12)
try:
root.destroy()
except:
pass
return user_input
if __name__ == '__main__':
diaginput = main()
您能帮我更改代码,以便我可以获取单选按钮的值,例如打印(诊断输入)。
我无法获取单选按钮的值。
【问题讨论】:
-
请修正缩进
-
要从单选按钮获取值,您不应使用
self.entry10左右,而应使用auswahl.get()。是的,您必须将其设为self才能在不同的功能中使用它
标签: python tkinter radio-button