Python 从Radiobutton获取输入
我混合使用Tkinter和graphics.py(Tkinter上的包装器)来创建一个具有GUI的相当基本的集成器(查找图形下的区域)。到目前为止,积分器的主“控制面板”与实际图形(使用graphics.py制作)是分开的。我使用Tkinter的Python 从Radiobutton获取输入,python,tkinter,Python,Tkinter,我混合使用Tkinter和graphics.py(Tkinter上的包装器)来创建一个具有GUI的相当基本的集成器(查找图形下的区域)。到目前为止,积分器的主“控制面板”与实际图形(使用graphics.py制作)是分开的。我使用Tkinter的Radiobutton()选择积分类型(左矩形、右矩形、梯形或辛普森近似) 我的问题是我无法从单选按钮获取输出。我使用的例子来自 这是我的密码: class FunctionInput: def __init__(self, master
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()
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()
问题是,由于我使用了多个其他小部件,我必须将
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.Rodas所说,为了消除灰色,我做了:
self.typeFrame = tk.Frame(self.frame)
self.typeChoice = tk.StringVar(self.typeFrame)
self.typeChoice.set(None)
设置变量不是问题,因为我还有大约6个其他小部件(混合了
Entry
s和Slide
s),它们也从用户那里获取输入,并且我使用相同的函数来获取输入。这些值已打印,但type_integration
始终打印为'
'。此外,我改变了我的结构以遵循您的(退出按钮设置变量),并尝试从退出按钮打印变量:除type_integration
外的所有变量打印正确。哦!我发现了问题所在。我必须创建StringVar
,主选项设置为self.typeFrame
,因为我在一个主框架中有许多其他框架。由于这个答案为您的问题提供了真正的解决方案,我鼓励您接受这个答案,而不是我的答案。