2015-07-10 75 views
2

我目前正在嘗試使用python在tkinter的幫助下構建一個GUI計算器。當我按下按鈕時(例如:按鈕5和5出現在條目())中,我設置了所有按鈕,並與我的輸入()欄互動。Python Tkinter條目()計算

剩下要做的唯一事情實際上是執行出現在entry()中的數學公式。例如,如果我在輸入欄中輸入5 + 5 * 2,我將如何在更新後將答案顯示在條目()中?所以基本上我所要求的是有人幫助我做到這一點!

我已經提供了下面的代碼和計算器的屏幕截圖。另外,請不要將此舉報爲重複或將此問題標記爲帖子,(我正在研究此問題,因爲我對最後一個問題沒有幫助,所以請幫助年輕的python程序員!)。另外我要求你不要給我提供鏈接,因爲我已經閱讀了tkinter上的每一個可能的鏈接,並且仍然不知道我在做什麼。

這裏是我的代碼:

import sys 
from tkinter import * 
from PIL import Image, ImageTk 

#ACTIONS: 
def clear(): 
    #Action: Clears the entry(). 
    txtDisplay.delete(0,END); 
    return; 

def update_Entry(inputValue): 
    #Updates the entry when a button is pressed. 
    currentText = num1.get(); 
    update = num1.set(currentText + inputValue); 

#Parent Window. 
root = Tk(); 
root.title('Calculator ++ [1.7.2]'); 
root.geometry('350x450'); 

#Main entry. 
num1 = StringVar(); 
txtDisplay = Entry(root, textvariable = num1, relief=RIDGE, bd = 10, width=33, insertwidth = 1, font = 40, justify=RIGHT); 
txtDisplay.place(x=15, y=10); 
txtDisplay.focus(); 

print(txtDisplay.get()) 

#Buttons: 
zeroButton = Button(root, text='0', width=20, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('0')); 
zeroButton.place(x=17,y=382); 
oneButton = Button(root, text='1', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('1')); 
oneButton.place(x=17, y=302); 
twoButton = Button(root, text='2', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('2')); 
twoButton.place(x=100, y=302); 
threeButton = Button(root, text='3', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('3')); 
threeButton.place(x=182, y=302); 
fourButton = Button(root, text='4', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('4')); 
fourButton.place(x=17, y=222); 
fiveButton = Button(root, text='5', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('5')); 
fiveButton.place(x=100, y=222); 
sixButton = Button(root, text='6', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('6')); 
sixButton.place(x=182, y=222); 
sevenButton = Button(root, text='7', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('7')); 
sevenButton.place(x=17, y=142); 
eightButton = Button(root, text='8', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('8')); 
eightButton.place(x=100, y=142); 
ninthButton = Button(root, text='9', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('9')); 
ninthButton.place(x=182, y=142); 

decimalButton = Button(root, text='.', width=8, height=3, bg='powder blue', command=lambda:update_Entry('.')); 
decimalButton.place(x=182, y=382); 
equalButton = Button(root, text='=', width=8, height=8, bg='Lightgreen', command=lambda:update_Entry('=')); 
equalButton.place(x=264, y=307); 
plusButton = Button(root, text='+', width=8, height=3, bg='gray', command=lambda:update_Entry('+')); 
plusButton.place(x=264, y=222); 
minusButton = Button(root, text='-', width=8, height=3, bg='gray', command=lambda:update_Entry('-')); 
minusButton.place(x=264, y=142); 
multiplyButton = Button(root, text='x', width=8, height=3, bg='gray', command=lambda:update_Entry('*')); 
multiplyButton.place(x=264, y=66); 
divideButton = Button(root, text='÷', width=8, height=3, bg='gray', command=lambda:update_Entry('/')); 
divideButton.place(x=182, y=66); 
clearButton = Button(root, text='Clear (CE)', width=20, height=3, command = clear, bg='Orange'); 
clearButton.place(x=17, y=66); 

#Locks the parent windows size. 
root.maxsize(350,450); 
root.minsize(350,450); 

#Parent window's background color: 
root.configure(background = 'black'); 
root.mainloop(); 

截圖:

Screenshot

+0

要注意,你不需要在Python中你的行尾加分號。閱讀[PEP 8](https://www.python.org/dev/peps/pep-0008/),學習像專家一樣格式化Python。 – Kupiakos

+0

@Kupiakos如果每行有多條語句,您將需要它們...... – nbro

+0

@ChristopherWallace您不應該這樣做。 – Kupiakos

回答

5

對於一些這個簡單,嘗試有此作爲command爲您equalButton

def evaluate(): 
    currentText = num1.get() 
    try: 
     num1.set(str(eval(currentText))) 
    except SyntaxError: 
     num1.set('<ERROR>') 
+0

謝謝這段代碼實際上爲我工作,並幫助我。只是最後一件事,我只想知道除了syntaxerror:語句做了什麼? –

+1

@PamalMangat它使得如果'eval' [拋出一個異常](https://docs.python.org/2/tutorial/errors.html)當試圖評估字符串,它會捕獲異常和向用戶顯示而不是崩潰整個程序。例如,嘗試輸入'2'然後輸入'+',然後輸入'='。 – Kupiakos