我使用 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 中有一个退出循环的按钮(此处未显示,因为它不相关)。没有引发错误,所以我猜测问题在于将选项设置为变量。非常感谢任何帮助。
另外,顺便说一下,每当我运行该程序时,“直角矩形”、“梯形”和“辛普森规则”单选按钮都会变灰,如下所示:
如果我单击其中一个单选按钮,这种灰色就会消失,但在那之前,它会一直存在。如果有办法解决这个问题,请告诉我。
谢谢!
最佳答案
我还没有看到退出按钮的部分,但你的代码做了这样的事情:
- 启动主循环
- 退出按钮的事件处理程序调用
root.quit()
- 在
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/