BeginnerSQL74651
BeginnerSQL74651

Reputation: 35

Tkinter - Retrieve text from button created in a loop

I'm learning to work with Python and Tkinter, so I'm doin this simple calculator app. My main problems come from the function calculator_tasks:

  1. In the first condition, it should be deleting the 0, but it doesn't do it, why is that?
  2. I'm trying to get text when I press the button, but it just prints '/', what's wrong in that?

Please see below the full code, so you might be able to help me.

from tkinter import *
import tkinter as tk


class calculator:
    def __init__(self, master):
        self.master = master
        master.title("Calculator")
        master.geometry("10x10")
        grid_size = 4
        a=0
        while a <= grid_size:
            master.grid_rowconfigure(a,weight=1, uniform='fred')
            master.grid_columnconfigure(a,weight=1, uniform='fred')
            a += 1
        self.ini_frame = Frame(master,bg="light blue",highlightthickness=2, highlightbackground="black")
        self.calc_title = Label(self.ini_frame, text ="Calculator",bg="light blue")
        self.calculation_frame = Frame(master,bg="black")
        self.calculation = IntVar(master,0)
        self.calc_figure = Text(self.calculation_frame,fg="white",bg='black')
        self.calc_figure.insert(END,0)
        self.ini_frame.grid(columnspan=4,sticky=NSEW)
        self.calculation_frame.grid(row=1,columnspan=4,sticky=NSEW)
        self.calc_title.grid(padx=20)
        self.calc_figure.grid(pady=20,padx=20)
        master.update_idletasks()
        
        def calculator_tasks():
            if self.calc_figure.get("1.0",END) == 0:
                self.calc_figure.delete(0,END)
                self.calc_figure.insert(END,i)
            else:
                self.calc_figure.insert(END,i)
        r = 2
        c = 0
        numbers = list(range(10))
        numbers.remove(0)
        self.calculator_btns=[ ]
        for i in numbers:   
            if c == 2:
                self.calculator_btns.append( Button(master, text = i, command = calculator_tasks))
                self.calculator_btns[-1].grid(row = r, column = c, pady=20, padx=20,sticky=NSEW)
                r += 1
                c = 0
            else:
                self.calculator_btns.append( Button(master, text = i,command=calculator_tasks))
                self.calculator_btns[-1].grid(row = r, column = c, pady=20, padx=20,sticky=NSEW)
                c += 1
        operators = ["+","*","/"]           
        self.operator_btns =[ ]
        r=2
        for i in operators: 
            self.operator_btns.append( Button(master, text = i))
            self.operator_btns[-1].grid(row = r, column = 3, pady=20, padx=20,sticky=NSEW)
            r += 1

root = Tk()
        
gui = calculator(root)
root.mainloop()

Upvotes: 0

Views: 270

Answers (1)

Manish Pushpam
Manish Pushpam

Reputation: 194

Here is a simple example:

According to your question: Retrieve text from button created in a loop

import tkinter as tk

root = tk.Tk()


####### Create Button ###############

def get_text(text):
    print(text)


for i in range(10):
    text = f'Hello I am BeginnerSQL74651 {i}'
    button = tk.Button(root, text=text, command=lambda button_text=text: get_text(button_text))
    button.grid()

root.mainloop()

Upvotes: 1

Related Questions