Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tkinter SyntaxError: keyword argument repeated: state (how to make a button one time clickable?)

from tkinter import *



def Click():
    label = Label(root, text="You clicked it")
    label.pack()

root = Tk()
label2 = Label(root, text="You can only click one time")
Button = Button(root, text="Click me", padx=20, pady=20, state=NORMAL, 
command=Click,state=DISABLED)
Button.pack()
label2.pack()
root.mainloop()

Because I use state 2 times I am getting this error:

SyntaxError: keyword argument repeated: state

How can I fix this error and make a button that can be clicked one time?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This should do what you want. The idea is to update the button’s state from within the click handler function.

FYI, star imports can cause trouble and are best avoided, so I’ve done that here. I’ve also changed some variable names to use lowercase, since capitalized names are typically reserved for class objects in Python (things like Label and Tk, for instance!)

import tkinter as tk


def click():
    label = tk.Label(root, text="You clicked it")
    label.pack()
    button.config(state=tk.DISABLED)  # disable the button here
    

root = tk.Tk()
label2 = tk.Label(root, text="You can only click one time")
button = tk.Button(root, text="Click me", padx=20, pady=20, command=click)
button.pack()
label2.pack()
root.mainloop()

Bonus Round – if you want to update the existing label (label2, that is) instead of creating a new label, you can also accomplish this with config

def click():
    label2.config(text="You clicked it")  # update the existing label
    button.config(state=tk.DISABLED)  # disable the button here
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading