1

Description: I would like to be able to click a button and have it send it's value to the label widget which is (lab_1), I understand to do one single button you have to use command=lambda: lab_1.configure(text=0) but how would you go about printing values to the label when I have used a for loop to create all of my buttons in an embedded list. as of right now all of the buttons have the variable named key_press.

Simplified Questions:

  • How do you write all the values to my label (lab_1)?

  • How can you use a for loop so every time that button is clicked it will display its value to the Label?


import tkinter as tk

root = tk.Tk()
# Change the original tkinter icon
root.iconbitmap('C:\\Users\\bmxfi\Downloads\images.ico')
# Set the title of the window
root.title('Calculator')
# Change the geometry of the window
root.geometry('300x450+750+150')
# Setting Minimum and Maximum window width and height
root.minsize(width=300, height=450)
root.maxsize(width=300, height=450)
# Create Rows and Columns to display widgets
root.rowconfigure(0, weight=100)
root.rowconfigure(1, weight=100)
root.rowconfigure(2, weight=100)
# Columns start here:
root.columnconfigure(0, weight=100)
root.columnconfigure(1, weight=100)
root.columnconfigure(2, weight=100)
# Configuring the Main window named: root
root.configure(bg='#353535')
# Creating the main label that will display the calculated results
lab_1 = tk.Label(root, width=40)
lab_1.grid(row=0, column=1, columnspan=1, sticky='we')
lab_1.configure(bg='#545454', font=('Computer Modern', 25, 'bold'),
                fg='White', justify='right')
# Main Calculator Layout
# List Box for a calculator layout
box_1 = tk.Listbox(root)
box_1.grid(row=1, column=1, rowspan=2)
box_1.configure(bg='#353535', relief='flat')
# Button Layout
calculator_layout = [[(7, 1), (8, 1), (9, 1), ('(', 1), (')', 1)],
                     [(4, 1), (5, 1), (6, 1), ('×', 1), ('÷', 1)],
                     [(1, 1), (2, 1), (3, 1), ('+', 1), ('-', 1)],
                     [(0, 1), ('±', 1), ('.', 1), ('-', 1), ('=', 1)]]

# Iterating through my list to get a set of buttons
row = 0
for i in calculator_layout:
    col = 0
    for a in i:
        key_press = tk.Button(box_1, text=a[0])
        key_press.grid(row=row, column=col, ipadx=10, ipady=8, sticky='we')
        key_press.configure(background='#545454', fg='white',
                            font=('Computer Modern', 10),
                            activebackground='red', state='normal')
        col += a[1]
    row += 1

root.mainloop()

1 Answer 1

1

You can do it by giving the lambda each Button an argument with a default value. There's no need for any extra for loops doing it this way (it's done in the one that creates the Buttons).

How to do that is shown below in the spot with the # CHANGED comment. The rest of the code in each lambda function defined each re-configures the text option of the Label named lab_1 by calling its configure() method and passing it the current contents of the text option with the symbol of the associated Button appended to it.

...

# Iterating through my list to get a set of buttons
row = 0
for i in calculator_layout:
    col = 0
    for a in i:
        key_press = tk.Button(box_1, text=a[0])
        key_press.grid(row=row, column=col, ipadx=10, ipady=8, sticky='we')
        key_press.configure(background='#545454', fg='white',
                            font=('Computer Modern', 10),
                            activebackground='red', state='normal',
                            # CHANGED.
                            command=lambda sym=str(a[0]):
                                lab_1.configure(text=lab_1.cget('text')+sym))
        col += a[1]
    row += 1

root.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much 😊 I have been struggling for days with this.
@Braiden: That's good to hear. Doing something like this sometimes called the "extra arguments trick" and is useful in several different situations such as yours when dealing with tkinter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.