M
M
max_1O2021-06-24 10:29:47
Python
max_1O, 2021-06-24 10:29:47

How to position multiple texts in different places in a label in tkinter?

Hello. I practice in python. I am currently trying to write a calculator in tkinter. Tell me how, after pressing the operation button (+, -, /, *), the entered early number was stored in a variable and this number moved up and changed the light from black to gray and under it the sign of the selected operation appeared, finally, below you could dial the number which must be subtracted, added, etc. Thanks in advance for your time. Whole script below

import tkinter as tk


def add_digit_to_lbl(digit):
    text = label["text"]
    label.configure(text=f"{text}"+str(digit))


window = tk.Tk()

entry_frame = tk.Frame()
label = tk.Label(master=entry_frame, width=30, height=5,
                 relief=tk.SUNKEN, borderwidth=5, anchor="se")

entry_frame.pack()
label.pack()

keys_frame = tk.Frame()
keys_frame.columnconfigure([i for i in range(4)], minsize=20)
keys_frame.rowconfigure([i for i in range(5)], minsize=10)
keys_frame.pack()

clear_btn = tk.Button(text="C", master=keys_frame, width=7,
                      command=lambda: label.delete(0, tk.END))
clear_btn.grid(column=0, row=0, ipady=15)

power_btn = tk.Button(text="^", master=keys_frame, width=7)
power_btn.grid(column=1, row=0, ipady=15)

addition_btn = tk.Button(text="+", master=keys_frame, width=7)
addition_btn.grid(column=2, row=0, ipady=15)

deduction_btn = tk.Button(text="-", master=keys_frame, width=7)
deduction_btn.grid(column=3, row=0, ipady=15)

one_btn = tk.Button(text="1", master=keys_frame, width=7,
                    command=lambda: add_digit_to_lbl(1))
one_btn.grid(column=0, row=1, ipady=15)

two_btn = tk.Button(text="2", master=keys_frame, width=7,
                    command=lambda: add_digit_to_lbl(2))
two_btn.grid(column=1, row=1, ipady=15)

three_btn = tk.Button(text="3", master=keys_frame, width=7,
                      command=lambda: add_digit_to_lbl(3))
three_btn.grid(column=2, row=1, ipady=15)

division_btn = tk.Button(text="/", master=keys_frame, width=7)
division_btn.grid(column=3, row=1, ipady=15)

four_btn = tk.Button(text="4", master=keys_frame, width=7,
                     command=lambda: add_digit_to_lbl(4))
four_btn.grid(column=0, row=2, ipady=15)

five_btn = tk.Button(text="5", master=keys_frame, width=7,
                     command=lambda: add_digit_to_lbl(5))
five_btn.grid(column=1, row=2, ipady=15)

six_btn = tk.Button(text="6", master=keys_frame, width=7,
                    command=lambda: add_digit_to_lbl(6))
six_btn.grid(column=2, row=2, ipady=15)

multiplication_btn = tk.Button(text="*", master=keys_frame, width=7)
multiplication_btn.grid(column=3, row=2, ipady=15)

seven_btn = tk.Button(text="7", master=keys_frame, width=7,
                      command=lambda: add_digit_to_lbl(7))
seven_btn.grid(column=0, row=3, ipady=15)

eight_btn = tk.Button(text="8", master=keys_frame, width=7,
                      command=lambda: add_digit_to_lbl(8))
eight_btn.grid(column=1, row=3, ipady=15)

nine_btn = tk.Button(text="9", master=keys_frame, width=7,
                     command=lambda: add_digit_to_lbl(9))
nine_btn.grid(column=2, row=3, ipady=15)

equality_btn = tk.Button(text="=", master=keys_frame, width=7)
equality_btn.grid(column=3, row=3, ipady=15)

window.mainloop()


PS
As you can see, the declaration of buttons, their output to the screen and their commands are repeated and take up a lot of space, I think that they can be simplified with cycles, but I don’t know how, can you advise something about this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-06-24
@Vindicar

Use multiple labels or draw the text yourself on the canvas.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question