Home>

I use python's Tkinter.
I was able to display multiple calculators on the screen, but if I displayed four at the same time, this time the calculator was only displayed and I couldn't calculate numbers without typing. How can I improve it so that four calculators can be displayed and used at the same time?
Now I write four codes below and four calculators are displayed, but I can't use the calculator function at the same time.

Error message

You can display multiple calculators but you cannot use the calculator at the same time

Applicable source code

`` Python Tkinter from tkinter import *

root = Tk ()

root.title ("Player1")

def func (v):
    var1.set (var1.get () + v)

def clear ():
    var1.set ("")

def all_clear ():
    var1.set ("")

def result ():
    try:
        var1.set (eval (var1.get ()))
    except SyntaxError:
        var1.set ("SyntaxError")
    except ZeroDivisionError:
        var1.set ("ZeroDivisionError")
    except NameError:
        var1.set ("NameError")

var1 = StringVar ()
label = Label (root, textvariable = var1, fg = "# ffffff", bg = "# 000000", anchor = E, height = 2)
label.grid (row = 0, column = 0, columnspan = 4, sticky = "EW")
#First row
btn_c = Button (root, text = "C", command = clear, width = 5, height = 2)
btn_c.grid (row = 1, column = 2)
btn_ac = Button (root, text = "AC", command = all_clear, width = 5, height = 2)
btn_ac.grid (row = 1, column = 3)
btn_add = Button (root, text = "+", command = lambda: func ("+"), width = 5, height = 2)
btn_add.grid (row = 2, column = 3)
# 2nd row
btn_7 = Button (root, text = "7", command = lambda: func ("7"), width = 5, height = 2)
btn_7.grid (row = 2, column = 0)
btn_8 = Button (root, text = "8", command = lambda: func ("8"), width = 5, height = 2)
btn_8.grid (row = 2, column = 1)
btn_9 = Button (root, text = "9", command = lambda: func ("9"), width = 5, height = 2)
btn_9.grid (row = 2, column = 2)
# 3rd row
btn_4 = Button (root, text = "4", command = lambda: func ("4"), width = 5, height = 2)
btn_4.grid (row = 3, column = 0)
btn_5 = Button (root, text = "5", command = lambda: func ("5"), width = 5, height = 2)
btn_5.grid (row = 3, column = 1)
btn_6 = Button (root, text = "6", command = lambda: func ("6"), width = 5, height = 2)
btn_6.grid (row = 3, column = 2)
btn_mul = Button (root, text = "*", command = lambda: func ("*"), width = 5, height = 2)
btn_mul.grid (row = 3, column = 3)
# 4th row
btn_1 = Button (root, text = "1", command = lambda: func ("1"), width = 5, height = 2)
btn_1.grid (row = 4, column = 0)
btn_2 = Button (root, text = "2", command = lambda: func ("2"), width = 5, height = 2)
btn_2.grid (row = 4, column = 1)
btn_3 = Button (root, text = "3", command = lambda: func ("3"), width = 5, height = 2)
btn_3.grid (row = 4, column = 2)
btn_sub = Button (root, text = "-", command = lambda: func ("-"), width = 5, height = 2)
btn_sub.grid (row = 4, column = 3)
# 5th column
btn_0 = Button (root, text = "0", command = lambda: func ("0"), width = 5, height = 2)
btn_0.grid (row = 5, column = 0)
btn_eq = Button (root, text = "=", command = result, width = 5, height = 2)
btn_eq.grid (row = 5, column = 3)
root.mainloop ()

Because I'm a beginner in programming, I didn't find any useful information to search.

Supplemental information (FW/tool version etc.)

Python 3.6.8 shell

  • Answer # 1

    Tkinter is not so detailed ...
    If you want to create more than one, you should define a class.
    If you want to create multiple top level windows, use the Toplevel component.

    I tried to display 4 other than the root window.
    All four exits when you close the root window.
    Information on which window is processed is passed to the argument self.

    from tkinter import *
    class Calc (Toplevel):
        def func (self, v):
            self.var1.set (self.var1.get () + v)
        def clear (self):
            self.var1.set ("")
        def all_clear (self):
            self.var1.set ("")
        def result (self):
            try:
                self.var1.set (eval (self.var1.get ()))
            except SyntaxError:
                self.var1.set ("SyntaxError")
            except ZeroDivisionError:
                self.var1.set ("ZeroDivisionError")
            except NameError:
                self.var1.set ("NameError")
        def __init __ (self, title):
            super () .__ init __ ()
            self.title (title)
            self.var1 = StringVar ()
            label = Label (self, textvariable = self.var1, fg = "# ffffff", bg = "# 000000", anchor = E, height = 2)
            label.grid (row = 0, column = 0, columnspan = 4, sticky = "EW")
            #First row
            btn_c = Button (self, text = "C", command = self.clear, width = 5, height = 2)
            btn_c.grid (row = 1, column = 2)
            btn_ac = Button (self, text = "AC", command = self.all_clear, width = 5, height = 2)
            btn_ac.grid (row = 1, column = 3)
            btn_add = Button (self, text = "+", command = lambda: self.func ("+"), width = 5, height = 2)
            btn_add.grid (row = 2, column = 3)
            # 2nd row
            btn_7 = Button (self, text = "7", command = lambda: self.func ("7"), width = 5, height = 2)
            btn_7.grid (row = 2, column = 0)
            btn_8 = Button (self, text = "8", command = lambda: self.func ("8"), width = 5, height = 2)
            btn_8.grid (row = 2, column = 1)
            btn_9 = Button (self, text = "9", command = lambda: self.func ("9"), width = 5, height = 2)
            btn_9.grid (row = 2, column = 2)
            # 3rd row
            btn_4 = Button (self, text = "4", command = lambda: self.func ("4"), width = 5, height = 2)
            btn_4.grid (row = 3, column = 0)
            btn_5 = Button (self, text = "5", command = lambda: self.func ("5"), width = 5, height = 2)
            btn_5.grid (row = 3, column = 1)
            btn_6 = Button (self, text = "6", command = lambda: self.func ("6"), width = 5, height = 2)
            btn_6.grid (row = 3, column = 2)
            btn_mul = Button (self, text = "*", command = lambda: self.func ("*"), width = 5, height = 2)
            btn_mul.grid (row = 3, column = 3)
            # 4th row
            btn_1 = Button (self, text = "1", command = lambda: self.func ("1"), width = 5, height = 2)
            btn_1.grid (row = 4, column = 0)
            btn_2 = Button (self, text = "2", command = lambda: self.func ("2"), width = 5, height = 2)
            btn_2.grid (row = 4, column = 1)
            btn_3 = Button (self, text = "3", command = lambda: self.func ("3"), width = 5, height = 2)
            btn_3.grid (row = 4, column = 2)
            btn_sub = Button (self, text = "-", command = lambda: self.func ("-"), width = 5, height = 2)
            btn_sub.grid (row = 4, column = 3)
            # 5th column
            btn_0 = Button (self, text = "0", command = lambda: self.func ("0"), width = 5, height = 2)
            btn_0.grid (row = 5, column = 0)
            btn_eq = Button (self, text = "=", command = self.result, width = 5, height = 2)
            btn_eq.grid (row = 5, column = 3)
    root = Tk ()
    calc1 = Calc ("# 1")
    calc2 = Calc ("# 2")
    calc3 = Calc ("# 3")
    calc4 = Calc ("# 4")
    root.mainloop ()

Related articles