Python Forum
[Tkinter] Button States
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Button States
#1
Script
from tkinter import *

root = Tk()

def my_fun():
  #make the my_btn state disabled
  #do some thing else

my_btn = Button(root, text = "CLICK ME", command = my_fun).pack()
How do I actually make my_fun do what it's supposed to do?
Reply
#2
You need to organize your code as class

import tkinter as tk

class App(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.root = root
        self.btn = tk.Button(root, text = "CLICK ME", command = self.my_callback)
        self.btn.pack()
        self.lbl = tk.Label(text = 'Click button...')
        self.lbl.pack()

    def my_callback(self):
        self.btn["state"] = tk.DISABLED
        self.lbl['text'] = 'Button clicked and disabled'

if __name__ == '__main__':
    root = tk.Tk()
    root.geometry("300x100")
    app = App(root)
    app.mainloop()
Oshadha likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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
#4
I think he just wants to know how to disable a button when clicked.

from tkinter import *
 
root = Tk()
 
def my_fun():
    #make the my_btn state disabled
    #do some thing else
    my_btn.config(state=DISABLED)

my_btn = Button(root, text = "CLICK ME", command = my_fun)
my_btn.pack()
The line you used with .pack on the end won't work in this scenario
so I changed that.

To re-enable the button change DISABLED to NORMAL.
Oshadha likes this post
Reply
#5
(Jan-27-2021, 11:31 AM)steve_shambles Wrote: I think he just wants to know how to disable a button when clicked.

from tkinter import *
 
root = Tk()
 
def my_fun():
    #make the my_btn state disabled
    #do some thing else
    my_btn.config(state=DISABLED)

my_btn = Button(root, text = "CLICK ME", command = my_fun)
my_btn.pack()
The line you used with .pack on the end won't work in this scenario
so I changed that.

To re-enable the button change DISABLED to NORMAL.
Tysm it worked!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,947 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