Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter issue
#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


Messages In This Thread
tkinter issue - by mate - Dec-06-2020, 07:13 PM
RE: tkinter issue - by deanhystad - Dec-06-2020, 07:27 PM
RE: tkinter issue - by mate - Dec-06-2020, 07:43 PM
RE: tkinter issue - by deanhystad - Dec-06-2020, 08:08 PM
RE: tkinter issue - by mate - Dec-06-2020, 09:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 3,349 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  Super basic tkinter arduino issue Kurta 3 2,517 Jan-07-2021, 05:22 PM
Last Post: deanhystad
  Issue in Tkinter with winfo_class() and LabelFrame ReDefendeur 1 2,815 Oct-05-2020, 05:52 AM
Last Post: Jeff900
  [Tkinter] tkinter: after issue edwin6938 1 3,505 Aug-25-2020, 04:37 PM
Last Post: Larz60+
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 2,229 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  [Tkinter] tkinter issue with variables carrying over between functions PengEng 1 1,817 Apr-06-2020, 06:13 PM
Last Post: deanhystad
  Issue on tkinter with buttons Reldaing 1 2,503 Jan-07-2020, 08:21 AM
Last Post: berckut72
  [Tkinter] Tkinter window issue frequency 4 3,464 Dec-24-2018, 10:49 AM
Last Post: frequency
  Tkinter positional information issue thatguy14 4 3,536 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