私はTkinterとgraphics.py(Tkinterのラッパー)を組み合わせて使用して、GUIでかなり基本的なインテグレーター(グラフの下の領域を見つける)を作成しています。現在のところ、インテグレーターのメインの「コントロールパネル」は、実際のグラフ(graphics.pyを使用して作成されたもの)とは別のものです。Radiobutton()
積分タイプ(左長方形、右長方形、台形、またはシンプソンの近似)の選択にTkinterを使用しています。
私の問題は、ラジオボタンからの出力を取得できないことです。TutorialsPointの例を使用していました:TkinterRadiobutton。
これが私のコードです:
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つをクリックすると消えますが、それまでは残ります。これを修正する方法があれば、私に知らせてください。
ありがとう!