from tkinter import * from tkinter import messagebox import random class Application(Frame): def init(self, master=None): super().init(master) self.master = master self.pack() self.createWidget() def createWidget(self): self.entry = Entry(self) self.entry.grid(row=0, column=0, columnspan=6, pady=10) btnText = (("MC", "M+", "M-", "MR"), ("C", "±", "/", "✖ "), (7, 8, 9, "-"), (4, 5, 6, "+"), (1, 2, 3, "="), (0, ".")) for rindex, r in enumerate(btnText): for cindex, c in enumerate(r): if c == "C": Button(self, text=c, width=2, command=self.clear) \ .grid(row=rindex + 1, column=cindex, sticky=NSEW) elif c == "=": Button(self, text=c, width=2, command=self.calculate) \ .grid(row=rindex + 1, column=cindex, rowspan=2, sticky=NSEW) elif c == 0: Button(self, text=c, width=2, command=lambda x=c: self.entry.insert(END, str(x))) \ .grid(row=rindex + 1, column=cindex, columnspan=2, sticky=NSEW) elif c == ".": Button(self, text=c, width=2, command=lambda x=c: self.entry.insert(END, str(x))) \ .grid(row=rindex + 1, column=cindex + 1, sticky=NSEW) else: Button(self, text=c, width=2, command=lambda x=c: self.entry.insert(END, str(x))) \ .grid(row=rindex + 1, column=cindex, sticky=NSEW) def clear(self): self.entry.delete(0, END) def calculate(self): try: result = eval(self.entry.get()) if isinstance(result, float): result = round(result, 2) if result < 0: self.entry.delete(0, END) self.entry.insert(END, "-") result = abs(result) self.entry.delete(0, END) self.entry.insert(END, str(result)) except: messagebox.showerror("Error", "Invalid expression") if name == 'main': root = Tk() root.geometry("200x200+200+300") app = Application(master=root) root.mainloop()使这串代码的✖能计算
时间: 2023-09-10 17:05:05 浏览: 130
用tkinter time random做的弹球小游戏.py
您可以将代码中的 "✖ " 替换为 "*",这样就可以让代码中的乘法符号正确计算了。修改后的代码如下所示:
```
from tkinter import *
from tkinter import messagebox
import random
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.createWidget()
def createWidget(self):
self.entry = Entry(self)
self.entry.grid(row=0, column=0, columnspan=6, pady=10)
btnText = (
("MC", "M+", "M-", "MR"),
("C", "±", "/", "*"),
(7, 8, 9, "-"),
(4, 5, 6, "+"),
(1, 2, 3, "="),
(0, ".")
)
for rindex, r in enumerate(btnText):
for cindex, c in enumerate(r):
if c == "C":
Button(self, text=c, width=2, command=self.clear) \
.grid(row=rindex + 1, column=cindex, sticky=NSEW)
elif c == "=":
Button(self, text=c, width=2, command=self.calculate) \
.grid(row=rindex + 1, column=cindex, rowspan=2, sticky=NSEW)
elif c == 0:
Button(self, text=c, width=2, command=lambda x=c: self.entry.insert(END, str(x))) \
.grid(row=rindex + 1, column=cindex, columnspan=2, sticky=NSEW)
elif c == ".":
Button(self, text=c, width=2, command=lambda x=c: self.entry.insert(END, str(x))) \
.grid(row=rindex + 1, column=cindex + 1, sticky=NSEW)
else:
Button(self, text=c, width=2, command=lambda x=c: self.entry.insert(END, str(x))) \
.grid(row=rindex + 1, column=cindex, sticky=NSEW)
def clear(self):
self.entry.delete(0, END)
def calculate(self):
try:
result = eval(self.entry.get())
if isinstance(result, float):
result = round(result, 2)
if result < 0:
self.entry.delete(0, END)
self.entry.insert(END, "-")
result = abs(result)
self.entry.delete(0, END)
self.entry.insert(END, str(result))
except:
messagebox.showerror("Error", "Invalid expression")
if __name__ == '__main__':
root = Tk()
root.geometry("200x200+200+300")
app = Application(master=root)
root.mainloop()
```
这样修改后,代码中的乘法符号就可以计算了。
阅读全文