1

私は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つをクリックすると消えますが、それまでは残ります。これを修正する方法があれば、私に知らせてください。

ありがとう!

4

2 に答える 2

1

終了ボタンの部分は見ていませんが、コードは次のようになります。

  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()
于 2013-03-09T15:56:29.137 に答える
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 に答える