Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter Entry()计算_Python_User Interface_Tkinter_Calculator_Python 3.4 - Fatal编程技术网

Python Tkinter Entry()计算

Python Tkinter Entry()计算,python,user-interface,tkinter,calculator,python-3.4,Python,User Interface,Tkinter,Calculator,Python 3.4,我目前正试图在tkinter的帮助下使用python构建一个GUI计算器。我已成功设置了所有按钮,当我按下按钮时,它们会与我的输入()栏交互(例如:按下按钮5,输入()中会出现5) 剩下要做的唯一一件事就是实际执行entry()中出现的数学方程。例如,如果我在输入栏中输入5+5*2,我将如何使答案在更新后显示在条目()中?所以基本上我所要求的就是有人帮我实现这个目标 我提供了下面的代码和计算器的屏幕截图。另外,请不要将此报告为重复报告或将此问题标记为帖子(我正在重新搜索此问题,因为我在上一个问题

我目前正试图在tkinter的帮助下使用python构建一个GUI计算器。我已成功设置了所有按钮,当我按下按钮时,它们会与我的输入()栏交互(例如:按下按钮5,输入()中会出现5)

剩下要做的唯一一件事就是实际执行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();
屏幕截图:


对于如此简单的内容,请尝试将其作为
命令
用于您的
equalButton

def evaluate():
    currentText = num1.get()
    try:
        num1.set(str(eval(currentText)))
    except SyntaxError:
        num1.set('<ERROR>')
def evaluate():
currentText=num1.get()
尝试:
num1.set(str(eval(currentText)))
除SyntaxError外:
num1.set(“”)

注意,在Python中,行尾不需要分号。阅读以学习像专家一样格式化Python。@Kupiakos如果每行有多条语句,您将需要它们…@ChristopherWallace您首先不应该这样做。@Kupiakos这只是一种风格,在我看来,它肯定不是最重要的。@ChristopherWallace绝对不是最重要的事情,但考虑到OP是个初学者,他不知道这一点是有道理的。最好在坏习惯形成之前,尽早地介绍正确的风格。谢谢,这段代码确实对我有用,并且帮助了我。最后一件事,我只想知道except-syntaxerror:语句做什么?@PamalMangat它这样做,如果
eval
在尝试计算字符串时,它将捕获异常并显示给用户,而不是使整个程序崩溃。例如,尝试输入
2
然后
+
然后
=