Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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和class的两个计算器不能正常工作_Python_Class_Tkinter_Calculator - Fatal编程技术网

Python 带tkinter和class的两个计算器不能正常工作

Python 带tkinter和class的两个计算器不能正常工作,python,class,tkinter,calculator,Python,Class,Tkinter,Calculator,我已经创建了一个可以用tkinter运行计算器的类,但是我只能运行一个实例。当运行两个实例时,一个计算器工作,另一个显示所有内容,但无法运行。如何使用该类同时创建不同的计算器。这是我的密码,谢谢 import tkinter as tk class calculator: def __init__(self, win): self.win = win self.win.configure(background="light green&qu

我已经创建了一个可以用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
,并将其传递给
类计算器的两个实例

实现这一点的一种方法是让
Calculator
tk.Toplevel
继承
我在根窗口上添加了一个
spawn按钮
——您可以生成任意数量的计算器实例(在合理范围内!)

请注意,
.grid()
方法返回
None
;不能在同一行上指定变量和网格

像这样:

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()

您只需要创建一个
tk.root
,并将其传递给
类计算器的两个实例

实现这一点的一种方法是让
Calculator
tk.Toplevel
继承
我在根窗口上添加了一个
spawn按钮
——您可以生成任意数量的计算器实例(在合理范围内!)

请注意,
.grid()
方法返回
None
;不能在同一行上指定变量和网格

像这样:

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()

谢谢,这是工作!!!那么,在没有“tk.Toplevel”的情况下,有可能做到这一点吗?或者创造两个“根”不是一个好的实践吗?谢谢,这是有效的!!!那么,在没有“tk.Toplevel”的情况下,有可能做到这一点吗?还是说创造两个“根”不是一个好的做法?