2017-06-27 10 views
0

私はPythonを使用して基本的な電卓を構築しました。しかし、Tkinterで実際のGUI電卓を作ろうとしました。問題は、値を取得する方法と特定の操作を返す方法がわからないことです。Tkinter GUI計算機エントリ値

  • 3エントリ(最初の値、第二の値、結果)
  • 4ボタン(加算、減算、乗算、除算):

    基本的に私のアプリは、このようなものです。

私のプログラムでは、最初のエントリに値を入力し、2番目のエントリに別の値を入力し、3番目のエントリで「差し引く」などの値をクリックすると、数字が表示されます。私はPythonには新しく、見つけたものは複雑すぎるようです。これはひどく書かれ

from tkinter import * 


def my_increase(a, b): 
    return a + b 


def my_decrease(a, b): 
    return a - b 


def my_multiply(a, b): 
    return a * b 


def my_divide(a, b): 
    return a/b 


main = Tk() 

label_1 = Label(main, text="Calculator", height=5, width=30) 
label_1.config(font=("OpenSans", 15)) 
label_1.pack() 
frame_0 = Frame(main) 
frame_0.pack() 
frame_1 = Frame(main) 
frame_1.pack() 
frame_2 = Frame(main) 
frame_2.pack() 

button_1 = Button(frame_1, text="Add", fg="blue", command=my_increase) 
button_1.config(font=("Lato", 12)) 
button_1.pack(side=LEFT) 
button_2 = Button(frame_1, text="Subtract", fg="blue", command=my_decrease) 
button_2.config(font=("Lato", 12)) 
button_2.pack(side=RIGHT) 
button_3 = Button(frame_2, text="Multiply", fg="blue", command=my_multiply) 
button_3.config(font=("Lato", 12)) 
button_3.pack(side=LEFT) 
button_4 = Button(frame_2, text="Divide", fg="blue", command=my_divide) 
button_4.config(font=("Lato", 12)) 
button_4.pack(side=RIGHT) 
entry_1 = Entry(frame_0, bd=5) 
entry_1.pack(side=TOP) 
entry_2 = Entry(frame_0, bd=5) 
entry_2.pack(side=TOP) 
entry_3 = Entry(frame_0, bd=5) 
entry_3.pack(side=TOP) 
main.mainloop() 
+1

エントリウィジェットから値を取得することに関連する多くの質問と回答があり、メソッド自体は十分に文書化されています。尋ねる前に、あなた自身でこの問題を解決しようとする研究をしましたか? –

答えて

1

各ボタンに付いている機能は、パラメータを必要としません。あなたの例では、my_increase関数をaとbの2つのパラメータで定義しています。しかし、ボタンをクリックすることによって、機能にパラメーターが渡されることはありません。代わりに、テキストボックスから値にアクセスして値を追加することができます。テキストボックスからアクセス値に

あなたがここ

entry_3.insert(0, "test") 

を使用することができ、テキストボックスに値を設定するには

entry_1.get() 

を使用することができますことは、加算、減算のようにすべての必要な操作を行い、あなたのコードです、。

from Tkinter import * 


def my_increase(): 
    entry_3.delete(0, END) 
    entry_3.insert(0, int(entry_1.get()) + int(entry_2.get())) 


def my_decrease(): 
    entry_3.delete(0, END) 
    entry_3.insert(0, int(entry_1.get()) - int(entry_2.get())) 


def my_multiply(): 
    entry_3.delete(0, END) 
    entry_3.insert(0, int(entry_1.get()) * int(entry_2.get())) 


def my_divide(): 
    entry_3.delete(0, END) 
    entry_3.insert(0, int(entry_1.get())/int(entry_2.get())) 


main = Tk() 

label_1 = Label(main, text="Calculator", height=5, width=30) 
label_1.config(font=("OpenSans", 15)) 
label_1.pack() 
frame_0 = Frame(main) 
frame_0.pack() 
frame_1 = Frame(main) 
frame_1.pack() 
frame_2 = Frame(main) 
frame_2.pack() 

button_1 = Button(frame_1, text="Add", fg="blue", command=my_increase) 
button_1.config(font=("Lato", 12)) 
button_1.pack(side=LEFT) 
button_2 = Button(frame_1, text="Subtract", fg="blue", command=my_decrease) 
button_2.config(font=("Lato", 12)) 
button_2.pack(side=RIGHT) 
button_3 = Button(frame_2, text="Multiply", fg="blue", command=my_multiply) 
button_3.config(font=("Lato", 12)) 
button_3.pack(side=LEFT) 
button_4 = Button(frame_2, text="Divide", fg="blue", command=my_divide) 
button_4.config(font=("Lato", 12)) 
button_4.pack(side=RIGHT) 
entry_1 = Entry(frame_0, bd=5) 
entry_1.pack(side=TOP) 
entry_2 = Entry(frame_0, bd=5) 
entry_2.pack(side=TOP) 
entry_3 = Entry(frame_0, bd=5) 
entry_3.pack(side=TOP) 
main.mainloop() 
0

、その提案のカップルを使用してtkinterのチュートリアルを確認してください。

  1. インポートのみあなたが
  2. 必要なものをクラスにすべてをかけます。 (__init__はTkオブジェクトを受け入れ、すべてのウィジェットを作成してループを開始する必要があります。スクリプトはクラスオブジェクトを初期化します)。何をしたい

  1. entry_3更新可能な利用を行うためにまず:

    result = DoubleVar()

    entry_3 = Entry(frame_0, bd=5,textvar=result)

    これはクラスでself.resultだろうので、あなたどんな方法からでもそれにアクセスできます。アクションメソッドで

  2. 、あなたは今ちょうど例の追加のために、この変数を設定します。クラスの

    result.set(a+b)

    またはself.result。こうすることで、グローバル変数を避けることができます。これは、returnステートメントを置き換える必要があります。どこにも戻っていません。

  3. ご覧のとおり、関数には2つの引数があります。ボタンを押すと、noneが送信されます。メソッド内の入力フィールドを自分でentry_1.get()で読み込み、引数を受け入れないでください。別のオプションは、プレス上にこれを送信することです:

    button_2 = Button(frame_1, text="Subtract", fg="blue", command=lambda: my_decrease(float(entry_1.get()),float(entry_2.get())))

は、私はオブジェクト(self.entry...)のentry_[1,2]分野元、およびアクセスとなるだろう。がんばろう。