python - tkinter 和 class 的两个计算器通常不能正常工作
问题描述
我已经创建了一个可以使用 tkinter 运行我的计算器的类,但我只能运行一个实例。运行两个实例时,一个计算器工作,另一个显示所有内容,但无法运行。如何使用此类同时创建不同的计算器。这是我的代码,谢谢。
import tkinter as tk
class calculator:
def __init__(self, win):
self.win = win
self.win.configure(background="light green")
self.win.title("Simple Calculator")
self.win.geometry("270x160")
self.expression = ''
self.equation = tk.StringVar()
self.equation.set('enter your expression')
self.expression_field = tk.Entry(self.win, textvariable=self.equation).grid(columnspan=4,
ipadx=70) # this is the display in calculator
self.button1 = tk.Button(self.win, text=' 1 ', fg='black', bg='red', command=lambda: self.press(1), height=1, width=7).grid(row=2, column=0)
self.button2 = tk.Button(self.win, text=' 2 ', fg='black', bg='red', command=lambda: self.press(2), height=1, width=7).grid(row=2, column=1)
self.button3 = tk.Button(self.win, text=' 3 ', fg='black', bg='red', command=lambda: self.press(3), height=1, width=7).grid(row=2, column=2)
self.button4 = tk.Button(self.win, text=' 4 ', fg='black', bg='red', command=lambda: self.press(4), height=1, width=7).grid(row=3, column=0)
self.button5 = tk.Button(self.win, text=' 5 ', fg='black', bg='red', command=lambda: self.press(5), height=1, width=7).grid(row=3, column=1)
self.button6 = tk.Button(self.win, text=' 6 ', fg='black', bg='red', command=lambda: self.press(6), height=1, width=7).grid(row=3, column=2)
self.button7 = tk.Button(self.win, text=' 7 ', fg='black', bg='red', command=lambda: self.press(7), height=1, width=7).grid(row=4, column=0)
self.button8 = tk.Button(self.win, text=' 8 ', fg='black', bg='red', command=lambda: self.press(8), height=1, width=7).grid(row=4, column=1)
self.button9 = tk.Button(self.win, text=' 9 ', fg='black', bg='red', command=lambda: self.press(9), height=1, width=7).grid(row=4, column=2)
self.button0 = tk.Button(self.win, text=' 0 ', fg='black', bg='red', command=lambda: self.press(0), height=1, width=7).grid(row=5, column=0)
self.plus = tk.Button(self.win, text=' + ', fg='black', bg='red', command=lambda: self.press("+"), height=1, width=7).grid(row=2, column=3)
self.minus = tk.Button(self.win, text=' - ', fg='black', bg='red', command=lambda: self.press("-"), height=1, width=7).grid(row=3, column=3)
self.multiply = tk.Button(self.win, text=' * ', fg='black', bg='red', command=lambda: self.press("*"), height=1, width=7).grid(row=4, column=3)
self.divide = tk.Button(self.win, text=' / ', fg='black', bg='red', command=lambda: self.press("/"), height=1, width=7).grid(row=5, column=3)
self.equal = tk.Button(self.win, text=' = ', fg='black', bg='red', command=self.equalpress, height=1, width=7).grid(row=5, column=2)
self.clear = tk.Button(self.win, text='Clear', fg='black', bg='red', command=self.clear, height=1, width=7).grid(row=5, column=1)
self.Decimal = tk.Button(self.win, text='.', fg='black', bg='red', command=lambda: self.press('.'), height=1, width=7).grid(row=6, column=0)
def press(self, num):
self.expression = self.expression + str(num)
self.equation.set(self.expression) # variable 'equation' is set in below code
def equalpress(self):
try:
total = str(eval(self.expression)) # perform action from string
self.equation.set(total)
self.expression = ""
except:
self.equation.set(" error ")
self.expression = ""
def clear(self):
self.expression = ""
self.equation.set("")
解决方案
您只需要创建一个tk.root
,并将其传递给class Calculator
.
实现此目的的一种方法是Calculator
继承自tk.Toplevel
.
我spawn button
在根窗口上添加了一个 - 您可以根据需要生成任意数量的计算器实例(在合理范围内!)。
请注意,.grid()
方法返回None
; 你不能分配给一个变量,并且grid
在同一行。
像这样:
import tkinter as tk
class Calculator(tk.Toplevel):
def __init__(self, title):
super().__init__(bg="light green")
self.title = title
self.geometry("270x160")
self.expression = ''
self.equation = tk.StringVar()
self.equation.set('enter your expression')
self.expression_field = tk.Entry(self, textvariable=self.equation)
self.expression_field.grid(columnspan=4, ipadx=70) # this is the display in calculator
self.button1 = tk.Button(self, text=' 1 ', fg='black', bg='red', command=lambda: self.press(1), height=1, width=7)
self.button1.grid(row=2, column=0)
self.button2 = tk.Button(self, text=' 2 ', fg='black', bg='red', command=lambda: self.press(2), height=1, width=7)
self.button2.grid(row=2, column=1)
self.button3 = tk.Button(self, text=' 3 ', fg='black', bg='red', command=lambda: self.press(3), height=1, width=7)
self.button3.grid(row=2, column=2)
self.button4 = tk.Button(self, text=' 4 ', fg='black', bg='red', command=lambda: self.press(4), height=1, width=7)
self.button4.grid(row=3, column=0)
self.button5 = tk.Button(self, text=' 5 ', fg='black', bg='red', command=lambda: self.press(5), height=1, width=7)
self.button5.grid(row=3, column=1)
self.button6 = tk.Button(self, text=' 6 ', fg='black', bg='red', command=lambda: self.press(6), height=1, width=7)
self.button6.grid(row=3, column=2)
self.button7 = tk.Button(self, text=' 7 ', fg='black', bg='red', command=lambda: self.press(7), height=1, width=7)
self.button7.grid(row=4, column=0)
self.button8 = tk.Button(self, text=' 8 ', fg='black', bg='red', command=lambda: self.press(8), height=1, width=7)
self.button8.grid(row=4, column=1)
self.button9 = tk.Button(self, text=' 9 ', fg='black', bg='red', command=lambda: self.press(9), height=1, width=7)
self.button9.grid(row=4, column=2)
self.button0 = tk.Button(self, text=' 0 ', fg='black', bg='red', command=lambda: self.press(0), height=1, width=7)
self.button0.grid(row=5, column=0)
self.plus = tk.Button(self, text=' + ', fg='black', bg='red', command=lambda: self.press("+"), height=1, width=7)
self.plus.grid(row=2, column=3)
self.minus = tk.Button(self, text=' - ', fg='black', bg='red', command=lambda: self.press("-"), height=1, width=7)
self.minus.grid(row=3, column=3)
self.multiply = tk.Button(self, text=' * ', fg='black', bg='red', command=lambda: self.press("*"), height=1, width=7)
self.multiply.grid(row=4, column=3)
self.divide = tk.Button(self, text=' / ', fg='black', bg='red', command=lambda: self.press("/"), height=1, width=7)
self.divide.grid(row=5, column=3)
self.equal = tk.Button(self, text=' = ', fg='black', bg='red', command=self.equalpress, height=1, width=7)
self.equal.grid(row=5, column=2)
self.clear = tk.Button(self, text='Clear', fg='black', bg='red', command=self.clear, height=1, width=7)
self.clear.grid(row=5, column=1)
self.Decimal = tk.Button(self, text='.', fg='black', bg='red', command=lambda: self.press('.'), height=1, width=7)
self.Decimal.grid(row=6, column=0)
def press(self, num):
self.expression = self.expression + str(num)
self.equation.set(self.expression) # variable 'equation' is set in below code
def equalpress(self):
try:
total = str(eval(self.expression)) # perform action from string
self.equation.set(total)
self.expression = ""
except:
self.equation.set(" error ")
self.expression = ""
def clear(self):
self.expression = ""
self.equation.set("")
root = tk.Tk()
numcalc = 1
tk.Button(root, text='spawn new calc', command=lambda: Calculator(f'calc no: {numcalc}')).pack()
root.mainloop()
推荐阅读
- wix - WIX-MSI:如果 msiexec 被杀死,密码启用卸载失败
- mysql - 如何修复 DATEDIFF 触发器中的语法错误
- javascript - Reactjs 中的 CSS 路径错误
- android - 自定义 PinEntryView - 默认符号
- git - 可以在 sourcetree 上使用 gitflow 和拉取请求吗?
- java - 将 Java 应用程序从 PC 连接到另一台 PC 上的 XAMPP 服务器
- python - 非正态分布数据中的异常值检测
- angular - 仅在为生产构建演示应用程序时获取“无法解析所有参数”
- vuejs2 - 如何使用 VueJS 和 Vuetify 有条件地显示文本字段的提示?
- django - Axios 不存储 Django 会话 cookie