我正在使用Python GUI创建一个简单的计算器,我已完成所有工作,但我不知道如何编写def Backspace(self):和def Plusminus(self)的代码。
现在,退格按钮正在删除数字或功能,但并未存储它,并且您按下所有按钮的下一个按钮又回来了。
如果数字大于0,则正号/负号应该使数字为负;如果数字小于0,则正号/负号应使它为正。现在,它只是在前面加上负号,而实际上并未使数字为负。
from tkinter import *
class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.task = ""
self.UserIn = StringVar()
self.grid()
self.create_widgets()
def create_widgets(self):
#Create all the buttons for calculator.
# User input stored as an Entry widget.
self.user_input = Entry(self, bg = "White",
insertwidth = 4, width = 24,
font = ("Times", 20), textvariable = self.UserIn, justify = RIGHT)
self.user_input.grid(columnspan = 4)
self.user_input.insert(0, "0")
# Button for value 7
self.button1 = Button(self, bg = "White",
text = "7", padx = 40, pady = 20, font = ("Times", 20),
command = lambda : self.buttonClick(7))
self.button1.grid(row = 2, column = 0, sticky = W)
# Button for value 8
self.button2 = Button(self, bg = "White",
text = "8", padx = 40, pady = 20,
command = lambda : self.buttonClick(8), font = ("Times", 20))
self.button2.grid(row = 2, column = 1, sticky = W)
# Button for value 9
self.button3 = Button(self, bg = "White",
text = "9", padx = 40, pady = 20,
command = lambda : self.buttonClick(9), font = ("Times", 20))
self.button3.grid(row = 2, column = 2, sticky = W)
# Button for value 4
self.button4 = Button(self, bg = "White",
text = "4", padx = 40, pady = 20,
command = lambda : self.buttonClick(4), font = ("Times", 20))
self.button4.grid(row = 3, column = 0, sticky = W)
# Button for value 5
self.button5 = Button(self, bg = "White",
text = "5", padx = 40, pady = 20,
command = lambda : self.buttonClick(5), font = ("Times", 20))
self.button5.grid(row = 3, column = 1, sticky = W)
# Button for value 6
self.button6 = Button(self, bg = "White",
text = "6", padx = 40, pady = 20,
command = lambda : self.buttonClick(6), font = ("Times", 20))
self.button6.grid(row = 3, column = 2, sticky = W)
# Button for value 1
self.button7 = Button(self, bg = "White",
text = "1", padx = 40, pady = 20,
command = lambda : self.buttonClick(1), font = ("Times", 20))
self.button7.grid(row = 4, column = 0, sticky = W)
# Button for value 2
self.button8 = Button(self, bg = "White",
text = "2", padx = 40, pady = 20,
command = lambda : self.buttonClick(2), font = ("Times", 20))
self.button8.grid(row = 4, column = 1, sticky = W)
# Button for value 3
self.button9 = Button(self, bg = "White",
text = "3", padx = 40, pady = 20,
command = lambda : self.buttonClick(3), font = ("Times", 20))
self.button9.grid(row = 4, column = 2, sticky = W)
# Button for value 0
self.button9 = Button(self, bg = "White",
text = "0", padx = 40, pady = 20,
command = lambda : self.buttonClick(0), font = ("Times", 20))
self.button9.grid(row = 5, column = 0, sticky = W)
# Operator buttons
# Addition button
self.Addbutton = Button(self, bg = "White",
text = "+", padx = 20, pady = 20,
command = lambda : self.buttonClick("+"), font = ("Times", 20))
self.Addbutton.grid(row = 2, column = 3, sticky = W)
# Subtraction button
self.Subbutton = Button(self, bg = "White",
text = "-", padx = 23, pady = 20,
command = lambda : self.buttonClick("-"), font = ("Times", 20))
self.Subbutton.grid(row = 3, column = 3, sticky = W)
# Multiplication button
self.Multbutton = Button(self, bg = "White",
text = "*", padx = 20, pady = 20,
command = lambda : self.buttonClick("*"), font = ("Times", 20))
self.Multbutton.grid(row = 4, column = 3, sticky = W)
# Division button
self.Divbutton = Button(self, bg = "White",
text = "/", padx = 23, pady = 20,
command = lambda : self.buttonClick("/"), font = ("Times", 20))
self.Divbutton.grid(row = 5, column = 3, sticky = W)
# Equal button
self.Equalbutton = Button(self, bg = "White",
text = "=", padx = 98, pady = 20,
command = self.CalculateTask, font = ("Times", 20))
self.Equalbutton.grid(row = 5, column = 1, sticky = W, columnspan = 2)
# Clear Button
self.Clearbutton = Button(self, bg = "White",
text = "C", font = ("Times", 20), padx = 38, pady = 20, command = self.ClearDisplay)
self.Clearbutton.grid(row = 1, columnspan = 2, sticky = W)
# Backspace Button
self.Backspace = Button(self, bg = "White",
text = "<", padx = 20, pady = 20,
command = self.Backspace, font = ("Times", 20))
self.Backspace.grid(row = 1, column = 3, sticky = W)
# Blank Button
self.blank = Button(self, bg = 'White',
text = ' ', padx = 44, pady = 20,
font = ('Times', 20))
self.blank.grid(row = 1, column = 1, sticky = W)
# +/- Button
self.Plusminus = Button(self, bg = 'White',
text = '+/-', padx = 31, pady = 20,
command = self.Plusminus, font = ('Times', 20))
self.Plusminus.grid(row = 1, column = 2, sticky = W)
def buttonClick(self, number):
self.task = str(self.task) + str(number)
self.UserIn.set(self.task)
def CalculateTask(self):
self.data = self.user_input.get()
try:
self.answer = eval(self.data)
self.displayText(self.answer)
self.task = self.answer
except SyntaxError as e:
self.displayText("Invalid Syntax!")
self.task = ""
def displayText(self, value):
self.user_input.delete(0, END)
self.user_input.insert(0, value)
def ClearDisplay(self):
self.task = ""
self.user_input.delete(0, END)
self.user_input.insert(0, "0")
def Backspace(self):
self.data = self.user_input.get()
self.user_input.delete(len(self.data)-1, END)
def Plusminus(self):
self.data = self.user_input.get()
if self.data.startswith('-'):
self.user_input.delete(0, 1) # delete the first character
else:
self.user_input.insert(0, '-') # add a - to the front
print(self.data)
print(self.task)
calculator = Tk()
calculator.title("My Calculator")
app = Application(calculator)
# Make window fixed (cannot be resized)
calculator.resizable(width = False, height = False)
calculator.mainloop()
答案 0 :(得分:0)
问题在于您不更新self.task
变量,而只修改用户输入字符串。
在self.user_input.insert(0, '-')
之后的Plusminus()中
self.task = self.user_input.get()
Backspace()中的同上。
我还注意到您使用self.Plusminus
作为函数和小部件的变量名。实际上,self.Plusminus
首先是指函数,而在创建小部件时,其后是指该函数。现在这不会给您造成问题,您应该避免这种情况。