从Radiobutton获取输入

时间:2013-03-09 05:26:49

标签: python tkinter

我正在混合使用Tkinter和graphics.py(Tkinter上的包装器)来创建一个相当基本的集成器(在图形下找到区域)和GUI。截至目前,积分器的主“控制面板”与实际图形(使用graphics.py制作)分开。我使用Tkinter的Radiobutton()来选择积分类型(左矩形,右矩形,梯形或辛普森近似)。

我的问题是我无法从radiobuttons获得输出。我正在使用TutorialsPoint: Tkinter Radiobutton中的示例。

这是我的代码:

class FunctionInput:
        def __init__(self, master, window, items):
            self.window = window
            self.items = items
            self.runProgram = True
            self.typeChoice = tk.StringVar()
            self.frame = tk.Frame(master)
            self.typeFrame = tk.Frame(self.frame)
            self.quitButton = tk.Button(self.frame, text = 'Quit', command = self.frame.quit)
            self.optLabel = tk.Label(self.frame, text = 'Type of Approximation: ')
            self.optL = tk.Radiobutton(self.typeFrame, text = 'Left Rectangular', variable = self.typeChoice, value = 'l')
            self.optR = tk.Radiobutton(self.typeFrame, text = 'Right Rectangular', variable = self.typeChoice, value = 'r')
            self.optT = tk.Radiobutton(self.typeFrame, text = 'Trapezoidal', variable = self.typeChoice, value = 't')
            self.optS = tk.Radiobutton(self.typeFrame, text = 'Simpsons Rule', variable = self.typeChoice, value = 's')
            self.optL.grid(row = 1, column = 1, padx = 5, pady = 5)
            self.optR.grid(row = 1, column = 2, padx = 5, pady = 5)
            self.optT.grid(row = 2, column = 1, padx = 5, pady = 5)
            self.optS.grid(row = 2, column = 2, padx = 5, pady = 5)
            self.optLabel.grid(row = 4)
            self.typeFrame.grid(row = 5)
            self.quitButton.grid(row = 6)
            # there were numerous other widgets and frames, but I only included the relevant ones
            self.frame.grid()

        def getInput(self):
            type_integration = self.typeChoice.get()
            self.frame.quit()
            return type_integration


def main():
    # some other code, win and axisLabels are defined prior to this
    root = tk.Tk(className = ' Function Grapher')
    app = FunctionInput(root, win, axisLabels)
    root.rowconfigure(0, weight = 1)
    root.columnconfigure(0, weight = 1)
    root.mainloop() # there is a button to exit the mainloop in my GUI
    typeIntegration = app.getInput()
    print typeIntegration # trying to debug it

if __name__ == '__main__': main()

但是,它不会打印变量。它打印一个空字符串,因此执行不是问题。 root.mainloop()不会陷入无限循环,因为我的GUI中有一个按钮(此处未显示,因为它无关紧要)退出它。不会引发错误,因此我猜测问题在于为变量设置选项。非常感谢任何帮助。

另外,在旁注中,每当我运行程序时,'Right Rectangular','Trapezoidal'和'Simpson's Rule'单选按钮都会变灰,如下所示:

如果我点击其中一个单选按钮,这种灰度会消失,但在此之前,它会停留。如果有办法解决这个问题,请告诉我。

谢谢!

2 个答案:

答案 0 :(得分:1)

我没有看到退出按钮的部分,但是你的代码做了类似的事情:

  1. 启动主界面
  2. 退出按钮的事件处理程序调用{​​{1}}
  3. root.quit()中,检索值并调用getInput
  4. 在mainloop之后调用Tkinter函数可能会导致这样的问题,因此您应首先获取StringVar的值,然后退出GUI循环。这是一个基于您的代码的工作示例:

    self.frame.quit()

答案 1 :(得分:1)

问题在于,由于我使用了多个其他小部件,因此我必须将StringVar()的{​​{1}}参数设置为master

self.typeFrame

另外,作为@A。罗达斯说,为了摆脱灰暗,我做了:

class FunctionInput:
        def __init__(self, master, window, items):
            self.window = window
            self.items = items
            self.runProgram = True
            self.frame = tk.Frame(master)
            self.typeFrame = tk.Frame(self.frame)
            self.typeChoice = tk.StringVar(self.typeFrame)
            self.quitButton = tk.Button(self.frame, text = 'Quit', command = self.frame.quit)
            self.optLabel = tk.Label(self.frame, text = 'Type of Approximation: ')
            self.optL = tk.Radiobutton(self.typeFrame, text = 'Left Rectangular', variable = self.typeChoice, value = 'l')
            self.optR = tk.Radiobutton(self.typeFrame, text = 'Right Rectangular', variable = self.typeChoice, value = 'r')
            self.optT = tk.Radiobutton(self.typeFrame, text = 'Trapezoidal', variable = self.typeChoice, value = 't')
            self.optS = tk.Radiobutton(self.typeFrame, text = 'Simpsons Rule', variable = self.typeChoice, value = 's')
            self.optL.grid(row = 1, column = 1, padx = 5, pady = 5)
            self.optR.grid(row = 1, column = 2, padx = 5, pady = 5)
            self.optT.grid(row = 2, column = 1, padx = 5, pady = 5)
            self.optS.grid(row = 2, column = 2, padx = 5, pady = 5)
            self.optLabel.grid(row = 4)
            self.typeFrame.grid(row = 5)
            self.quitButton.grid(row = 6)
            # there were numerous other widgets and frames, but I only included the relevant ones
            self.frame.grid()

        def getInput(self):
            type_integration = self.typeChoice.get()
            self.frame.quit()
            return type_integration


def main():
    # some other code, win and axisLabels are defined prior to this
    root = tk.Tk(className = ' Function Grapher')
    app = FunctionInput(root, win, axisLabels)
    root.rowconfigure(0, weight = 1)
    root.columnconfigure(0, weight = 1)
    root.mainloop() # there is a button to exit the mainloop in my GUI
    print app.getInput()

if __name__ == '__main__': main()