Home > Blockchain >  How do I create an interactive label in Tkinter in Python without using OOP?
How do I create an interactive label in Tkinter in Python without using OOP?

Time:08-12

I have less than 15 hours of coding experience under my belt and have created a few basic projects, so dont kill me for asking a stupid question.

Basically I'm trying to create a calculator in Tkinter in Python, and I don't want to use OOP since apparently it makes your life 10x harder with all the different classes and stuff. For the label at the top of the window, I have no idea how to make it interactive so the user can input more than 1 number. I tried doing var.set("whatever the number is") for each command for each button but that just ends up replacing the entire label! this is what i've got so far:

from cgi import print_directory
from pydoc import classname
from tkinter import *
e = Tk(className="Krishna's Calculator")
e.geometry("460x614")
e.resizable(0,0)
def edivide():
    var.set("÷")
def etimes():
    var.set("×")
def eaddition():
    var.set(" ")
def esubtraction():
    var.set("-")
def edecimal():
    var.set(".")
def equals():
    var.set("=")
def clear():
    var.set("")
def e0():
    var.set("0")
def e1():
    var.set("1")
def e2():
    var.set("2")
def e3():
    var.set("3")
def e4():
   var.set("4")
def e5():
    var.set("5")
def e6():
    var.set("6")
def e7():
    var.set("7")
def e8():
    var.set("8")
def e9():
    var.set("9")
def epercent():
    var.set("%")
divide = Button(e, text = "÷",width=4, command = edivide, font = "Helvetica 50", highlightbackground='#8533ff')
divide.place(x = 344, y = 301)
multiply = Button(e, text = "×",width=4, command = etimes, font = "Helvetica 50", highlightbackground='#8533ff')
multiply.place(x = 344, y = 364)
minus = Button(e, text = "-",width=4, command = esubtraction, font = "Helvetica 50", highlightbackground='#8533ff')
minus.place(x = 344, y = 426)
plus = Button(e, text = " ",width=4, command = edecimal, font = "Helvetica 50", highlightbackground='#8533ff')
plus.place(x = 344, y = 488)
equal = Button(e, text = "=",width=4, command = equals, font = "Helvetica 50", highlightbackground='#8533ff')
equal.place(x = 344, y = 551)
aclear = Button(e, text = "AC",width=8, command = clear, font = "Helvetica 50",highlightbackground='#737373')
aclear.place(x = 0, y = 302)
percentage = Button(e, text = "%",width=4, command = epercent, font = "Helvetica 50",highlightbackground='#737373')
percentage.place(x = 228, y = 302)
decimal = Button(e, text = ".",width=4, command = edecimal, font = "Arial 50 bold",highlightbackground='#ffffff')
decimal.place(x = 228, y = 550)
number0 = Button(e, text = "0",width = 8, command = e0, font = "Arial 50 bold",highlightbackground='#ffffff')
number0.place(x = 0, y = 550)
number1 = Button(e, text = "1",width =4, command = e1, font = "Arial 50 bold",highlightbackground='#ffffff')
number1.place(x = 0, y = 488)
number2 = Button(e, text = "2",width =4, command = e2, font = "Arial 50 bold",highlightbackground='#ffffff')
number2.place(x = 114, y = 488)
number3 = Button(e, text = "3",width=4, command = e3, font = "Arial 50 bold",highlightbackground='#ffffff')
number3.place(x = 228, y = 488)
number4 = Button(e, text = "4",width=4, command = e4, font = "Arial 50 bold",highlightbackground='#ffffff')
number4.place(x = 0, y = 426)
number5 = Button(e, text = "5",width=4, command = e5, font = "Arial 50 bold",highlightbackground='#ffffff')
number5.place(x = 114, y = 426)
number6 = Button(e, text = "6",width=4, command = e6, font = "Arial 50 bold",highlightbackground='#ffffff')
number6.place(x = 228, y = 426)
number7 = Button(e, text = "7",width=4, command = e7, font = "Arial 50 bold" ,highlightbackground='#ffffff')
number7.place(x = 0, y = 364)
number8 = Button(e, text = "8",width=4, command = e8, font = "Arial 50 bold",highlightbackground='#ffffff')
number8.place(x = 114, y = 364)
number9 = Button(e, text = "9",width=4, command = e9, font = "Arial 50 bold",highlightbackground='#ffffff')
number9.place(x = 228, y = 364)
var = StringVar()
label = Label(e, textvariable = var,bd=5,width=16, relief = SOLID, font = "Arial 50 ",bg="white", fg="black",activebackground="#bb99ff", height = 5,pady = 3)
label.place(x=0,y=0),
e.mainloop()

if you run it you get a window with a calculator in it and if you click on a button, then that character will appear in the label, but if you click another button, then the previous character dissapears and the new one appears.

Basically I want to know how to keep the previous character and not make it disappear. (without OOP!)

CodePudding user response:

There are a few issues with the code you posted, but first, you need to use var.set(var.get() "value").

To reduce a the size of your file largely, use one function to insert, i.e.:

def insert(value):
    var.set(var.get()   value)

You can then use lambdas in your command parameter: command=lambda: insert("text").

Next, store your fonts and colours in a variable so you can just change one to change all.
The final change I will suggest is to create a utility function to create buttons for you, i.e.:

def button(text, width, font, highlightbackground, x, y):
    tkinter_button = Button(e, text=text, width=width, command=lambda: insert(text), font=font, highlightbackground=highlightbackground)
    tkinter_button.place(x=x, y=y)

Of course, you could remove some more parameters to lead to less duplicated code, but I'll leave that for you to do.

Here is the code with all of these changes except for the colours being in a variable; I suggest you do that as an exercise in refactoring code.

from tkinter import *
e = Tk(className="Krishna's Calculator")
e.geometry("460x614")
e.resizable(0,0)

def insert(value):
    var.set(var.get()   value)

def button(text, width, font, highlightbackground, x, y):
    tkinter_button = Button(e, text=text, width=width, command=lambda: insert(text), font=font, highlightbackground=highlightbackground)
    tkinter_button.place(x=x, y=y)

helvetica = "Helvetica 50"
arial = "Arial 50 bold"

button(text = "÷", width=4, font = helvetica, highlightbackground='#8533ff', x = 344, y = 301)
button(text = "×", width=4, font = helvetica, highlightbackground='#8533ff', x = 344, y = 364)
button(text = "-", width=4, font = helvetica, highlightbackground='#8533ff', x = 344, y = 426)
button(text = " ", width=4, font = helvetica, highlightbackground='#8533ff', x = 344, y = 488)
button(text = "=", width=4, font = helvetica, highlightbackground='#8533ff', x = 344, y = 551)
button(text = "AC", width=8, font = helvetica,highlightbackground='#737373', x = 0, y = 302)
button(text = "%", width=4, font = helvetica,highlightbackground='#737373', x = 228, y = 302)
button(text = ".", width=4, font = arial,highlightbackground='#ffffff', x = 228, y = 550)
button(text = "0", width = 8, font = arial,highlightbackground='#ffffff', x = 0, y = 550)
button(text = "1", width =4, font = arial,highlightbackground='#ffffff', x = 0, y = 488)
button(text = "2", width =4, font = arial,highlightbackground='#ffffff', x = 114, y = 488)
button(text = "3", width=4, font = arial,highlightbackground='#ffffff', x = 228, y = 488)
button(text = "4", width=4, font = arial,highlightbackground='#ffffff', x = 0, y = 426)
button(text = "5", width=4, font = arial,highlightbackground='#ffffff', x = 114, y = 426)
button(text = "6", width=4, font = arial,highlightbackground='#ffffff', x = 228, y = 426)
button(text = "7", width=4, font = arial ,highlightbackground='#ffffff', x = 0, y = 364)
button(text = "8", width=4, font = arial,highlightbackground='#ffffff', x = 114, y = 364)
button(text = "9", width=4, font = arial,highlightbackground='#ffffff', x = 228, y = 364)
var = StringVar()
label = Label(e, textvariable = var,bd=5,width=16, relief = SOLID, font = "Arial 50 ",bg="white", fg="black",activebackground="#bb99ff", height = 5,pady = 3)
label.place(x=0,y=0)
e.mainloop()

You can see how much smaller that code sample is than yours already.

  • Related