Python Forum
[Tkinter] Button States
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Button States
#3
What do you think my_btn is after this code executes? If you don't know you should find out.
my_btn = Button(root, text = "CLICK ME", command = my_fun).pack()
Classes are a good idea for GUI applications because they combine functions with a private namespace that holds all the related variables. It makes for a very clean solution. But classes are not required. Below is an example that passes the required information as an argument to the callback.
import random
from tkinter import *

colors = ['red', 'green', 'blue', 'orange', 'yellow']

def my_fun(button):
  button['fg'] = random.choice(colors)
 
root = Tk()
b1 = Button(root, text = "CLICK ME")
b1.configure(command = lambda : my_fun(b1))
b1.pack()

b2 = Button(root, text = "ME TOO")
b2.configure(command = lambda : my_fun(b2))
b2.pack()
Oshadha likes this post
Reply


Messages In This Thread
Button States - by Oshadha - Jan-20-2021, 03:15 PM
RE: Button States - by buran - Jan-20-2021, 04:03 PM
RE: Button States - by deanhystad - Jan-20-2021, 06:32 PM
RE: Button States - by steve_shambles - Jan-27-2021, 11:31 AM
RE: Button States - by Oshadha - Feb-01-2021, 03:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,112 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

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