python - 从单选按钮获取输入

标签 python tkinter

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

我的问题是我无法从单选按钮获取输出。我使用 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 中有一个退出循环的按钮(此处未显示,因为它不相关)。没有引发错误,所以我猜测问题在于将选项设置为变量。非常感谢任何帮助。

另外,顺便说一下,每当我运行该程序时,“直角矩形”、“梯形”和“辛普森规则”单选按钮都会变灰,如下所示:

如果我单击其中一个单选按钮,这种灰色就会消失,但在那之前,它会一直存在。如果有办法解决这个问题,请告诉我。

谢谢!

最佳答案

我还没有看到退出按钮的部分,但你的代码做了这样的事情:

  1. 启动主循环
  2. 退出按钮的事件处理程序调用root.quit()
  3. getInput 中,检索值并调用 self.frame.quit()

在主循环之后调用 Tkinter 函数可能会导致此类问题,因此您应该先获取 StringVar 的值,然后退出 GUI 循环。这是一个基于您的代码的工作示例:

import Tkinter as tk

class App():
    def __init__(self, master):
        self.master = master
        self.type_integration = None
        self.typeChoice = tk.StringVar()
        self.typeChoice.set(None) # This fixes the grayness of the radio buttons!
        self.typeFrame = tk.Frame(master)
        OPTIONS = [('Left Rectangular', 'l'),
                   ('Right Rectangular', 'r'),
                   ('Trapezoidal', 't'),
                   ('Simpsons Rule', 's')]
        for text, value in OPTIONS:
            tk.Radiobutton(self.typeFrame, text=text, variable=self.typeChoice, value=value).pack()
        tk.Button(self.typeFrame, text="Exit", command=self.exit).pack()
        self.typeFrame.pack()
    def exit(self):
        self.type_integration = self.typeChoice.get()
        self.master.destroy() # self.master.quit() freezes the GUI
    def getinput(self):
        return self.type_integration

master = tk.Tk()
app = App(master)
tk.mainloop()
print app.getinput()

关于python - 从单选按钮获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15307531/

相关文章:

python - 为什么 termcolor 在 Windows 控制台中输出的是控制字符而不是彩色文本?

python - 将列表拆分为值上的嵌套列表

python - 计算深度神经网络关于输入的偏导数

python - Numpy 优化

python - Tkinter 数学测验

python - 客户端,Google Flex App Engine和Cloud SQL之间的高延迟

python - Py2app - 将 "from x import y"添加到 setup.py

python - Tkinter Text 小部件 - 通过 y 坐标识别行

python - 如何使用 tkinter 将按钮移出他的 parent ?

python - 如何隐藏 tkinter ttk.Notebook 小部件中的整个选项卡栏?