首页 > 解决方案 > 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("")

标签: pythonclasstkintercalculator

解决方案


您只需要创建一个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()

推荐阅读