【问题标题】:Python tkinter Radio Buttons "On Submit"Python tkinter 单选按钮“提交时”
【发布时间】: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


【解决方案1】:

您不应该尝试访问单选按钮,而是访问用于存储值的变量。 但是你应该使变量成为对话框类的属性,以便从所有方法中访问它:

    self.auswahl = IntVar()
    self.auswahl.set(0)
    
    self.entry10 = Radiobutton(frame10, text="No",variable=self.auswahl, padx = 0, value=0).pack(anchor=W)        
    self.entry11 = Radiobutton(frame10, text="Sometimes",variable=self.auswahl,padx = 0, value=1).pack(anchor=W)
    self.entry12 = Radiobutton(frame10, text="Yes",variable=self.auswahl,padx = 0, value=2).pack(anchor=W)  

在提交的函数中,你可以只获取.auswahl属性的值

    # Radio-Buttons
    self.output = self.auswahl.get()

然后变量self.output 将保存一个值在0 到2 之间的整数,具体取决于您的选择。

【讨论】:

  • 应该是“介于0和2之间的值”
  • 所以为了避免混淆,最好将值设置为 -1 而不是 0:self.auswahl.set(-1)。或者如果没有选择任何内容,它将指向 0。
  • @CoolCloud:这就是为什么我假设按钮上的值从 1 变为 3。这也是我在创建 RB 时使用的基本原理。但可能在上下文中预先选择其中一个选项是有意义的。
  • 感谢您的帮助!它现在正在工作。此外,我必须添加更改 self.output9 而不是 self.output
猜你喜欢
  • 2020-06-19
  • 2016-09-11
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2014-03-16
  • 2016-09-01
  • 1970-01-01
  • 2018-05-12
相关资源
最近更新 更多