1

我正在使用 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 中有一个按钮(此处未显示,因为它无关紧要)退出它。没有引发错误,所以我猜测问题在于将选项设置为变量。任何帮助是极大的赞赏。

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

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

谢谢!

4

2 回答 2

1

问题是,由于我使用了多个其他小部件,因此我必须将StringVar()smaster参数设置为self.typeFrame

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()

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

self.typeFrame = tk.Frame(self.frame)
self.typeChoice = tk.StringVar(self.typeFrame)
self.typeChoice.set(None)
于 2013-03-09T16:45:10.297 回答
1

我还没有看到退出按钮的部分,但是您的代码执行以下操作:

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

在 mainloop 之后调用 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()
于 2013-03-09T15:56:29.137 回答