0

I want to create a button that I can disable and re-enable as soon as I click it. With the following code, I get a button that I can disable, but it won't re-enable when I click it. If looked at this threat, but it didn't help: Disable / Enable Button in TKinter

import tkinter
from tkinter import *

# toplevel widget of Tk which represents mostly the main window of an application
root = tkinter.Tk()
root.geometry('1800x600')
root.title('Roll Dice')
frame = Frame(root)

# label to display dice
label = tkinter.Label(root, text='', font=('Helvetica', 120))

# function activated by button
def switch1():
    if button1["state"] == "active":
        button1["state"] = "disabled"
    else:
        button1["state"] = "active"

button1 = tkinter.Button(root, text='Würfel 1', foreground='green', command=lambda: switch1, state = "active")

button1.pack(side = LEFT)

root.mainloop()
Mazze
  • 383
  • 3
  • 13
  • 1
    Instead of `"active"` use `"normal"` – TheLizzard May 02 '21 at 09:20
  • 2
    Also instead of `command=lambda: switch1` use `command=switch1`. Also also you know that when the button is disabled it can't be clicked so the user can't re enable it. What exactly are you trying to do? – TheLizzard May 02 '21 at 09:22
  • I wanted to disbale and re-enable a function by clicking on the button. I guess I try it with a variable for True and False – Mazze May 02 '21 at 15:15
  • You can add another button that toggles `button1`'s state. What exactly are you trying to do? Why do you want to toggle the button? – TheLizzard May 02 '21 at 15:17
  • I want to disbale and re-enable a function by clicking on the Button. – Mazze May 02 '21 at 15:19
  • I will try it with another button to toggle button1 – Mazze May 02 '21 at 15:21
  • You might have made a typo in the lambda statement fixing it shall yield fine results as expected. – typedecker May 02 '21 at 15:40

1 Answers1

1

Look at this:

import tkinter as tk

def toggle_state():
    if button1.cget("state") == "normal":
        button1.config(state="disabled")
    else:
        button1.config(state="normal")

root = tk.Tk()

button1 = tk.Button(root, text="This button toggles state")
button1.pack()

button2 = tk.Button(root, text="Click me", command=toggle_state)
button2.pack()

root.mainloop()

This uses a button to toggle the state of the other button.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • 1
    If anyone is crazy and wants a oneliner ~ `command=lambda: button1.config(state="disabled") if button1.cget("state") == "normal" else button1.config(state="normal")`. – Delrius Euphoria May 02 '21 at 15:36
  • @CoolCloud Nice 1 liner. I never knew that you can put `if` statements inside `lambda`s :D – TheLizzard May 02 '21 at 15:39
  • 1
    Yes, but it single handedly violated PEP8's rule of 80 characters in one line :P – Delrius Euphoria May 02 '21 at 15:40
  • 1
    @CoolCloud Also the zen of python [PEP 20](https://www.python.org/dev/peps/pep-0020/). Lets see how many PEPs you can violate with 1 line of code :D – TheLizzard May 02 '21 at 15:42
  • thanks that works. The one liner get's me confused :D – Mazze May 02 '21 at 15:42
  • @Mazze It is basically the same thing that I have in `toggle_state` but condensed into 1 line. Most of the time it is hard to understand/debug that is why it isn't considered good. – TheLizzard May 02 '21 at 15:44