Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter issue
#1
Photo 
Hi guys,

I'm having a problem creating multiple buttons in tkinter using a for loop. I just can't figure out how to make each button unique. First task is to fill a 9x9 grid with buttons, which is successfully acomplished by the command below.
def grid_fill():
    a=14
    b=15
    for j in range(9):
        for i in range(9):
            buttons = Button(main, text = 0, width = 7, height = 3)
            buttons.place(x = a, y = b)
            a+=76.7
        a = 14
        b+=76.7
But my goal is not only to place these buttons but also assign each one a different command. The code above just places 81 buttons which are all the same. Any suggestions?

Thank you very much for all of your help in advance. Big Grin
Larz60+ write Dec-06-2020, 10:26 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags in future posts.
Reply
#2
I don't know how much benefit there is in making buttons in a loop when they are bound to different functions. Unless the functions are all the same function with different arguments.
from tkinter import *

def grid_fill(frame):
    colors = ['red', 'green']
    k = 0
    for j in range(5):
        for i in range(5):
            k += 1
            color = colors[i % len(colors)]
            b = Button(frame, text = str(k), width = 3, height = 2, bg=color)
            b.configure(command=lambda x=k: print(x))
            b.grid(row=j, column=i)

grid_fill(Tk())
mainloop()
I'm doing three things here
1. Calculating the button label.
2. Looking up the button color.
3. Setting a callback with button specific arguments.
mate likes this post
Reply
#3
OH! loking at your code I can now see what I was doing wrong. I misunderstood the lambda command but I think I understand now.
Thank you very much!





(Dec-06-2020, 07:27 PM)deanhystad Wrote: I don't know how much benefit there is in making buttons in a loop when they are bound to different functions. Unless the functions are all the same function with different arguments.
from tkinter import *

def grid_fill(frame):
    colors = ['red', 'green']
    k = 0
    for j in range(5):
        for i in range(5):
            k += 1
            color = colors[i % len(colors)]
            b = Button(frame, text = str(k), width = 3, height = 2, bg=color)
            b.configure(command=lambda x=k: print(x))
            b.grid(row=j, column=i)

grid_fill(Tk())
mainloop()
I'm doing three things here
1. Calculating the button label.
2. Looking up the button color.
3. Setting a callback with button specific arguments.
Reply
#4
Were you doing something like this?
def grid_fill(frame):
    colors = ['red', 'green']
    k = 0
    for j in range(5):
        for i in range(5):
            k += 1
            color = colors[i % len(colors)]
            b = Button(frame, text = str(k), width = 3, height = 2, bg=color)
            b.configure(command=lambda : print(k))
            b.grid(row=j, column=i)
And when you pushed any button the callback function used the last value assigned to k?

This is because all the functions are bound to "lambda : print(k)" where "k" is a variable defined in the "grid_fill()" name space. All the lambda's are using the same "k". This is an unusual function variable in that it is not deleted as soon as the function ends. Since it is referenced multiple times it is not reclaimed by the garbage collection.

In my code I declared a variable for the lambda and initialized the value. It is very much like I wrote this:
def print_number(number = 5):
    print(number)
Where "5" is the value of "k" for that particular iteration.
Reply
#5
Well I'm trying to make a program that solves sudoku where you input numbers and press button 'solve' and then it fills the rest of the empty spots. I thought I would make it this way : 9x9 grid filled with buttons with text = 0, and whenever you press a button its text changes to one above so you can easily input numbers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 2,969 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  Super basic tkinter arduino issue Kurta 3 2,376 Jan-07-2021, 05:22 PM
Last Post: deanhystad
  Issue in Tkinter with winfo_class() and LabelFrame ReDefendeur 1 2,706 Oct-05-2020, 05:52 AM
Last Post: Jeff900
  [Tkinter] tkinter: after issue edwin6938 1 3,348 Aug-25-2020, 04:37 PM
Last Post: Larz60+
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 2,120 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  [Tkinter] tkinter issue with variables carrying over between functions PengEng 1 1,703 Apr-06-2020, 06:13 PM
Last Post: deanhystad
  Issue on tkinter with buttons Reldaing 1 2,415 Jan-07-2020, 08:21 AM
Last Post: berckut72
  [Tkinter] Tkinter window issue frequency 4 3,298 Dec-24-2018, 10:49 AM
Last Post: frequency
  Tkinter positional information issue thatguy14 4 3,375 Jul-05-2018, 06:49 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020