0

Can anyone tell me how to make if statement if the text of button is equal example like this code

from tkinter import *

app = Tk()

def k():
    #i know this is wrong so plss correct me
    if button1.text == "1":
        l = Label(app,text="wow")
        l.pack
    
button1 = Button(app,text="1",command=k).pack()

app.mainloop()

Plss tell me thank you

  • 1
    May I know what you are trying to do with the if statement? – Desty Feb 21 '22 at 05:47
  • 1
    Note that `button1` is `None` due to this [issue](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name). You can get the *text* of the button using `button1['text']` or `button1.cget('text')` (of course after fixing the issue). – acw1668 Feb 21 '22 at 05:55
  • Im making an tic tac toe program and i need the if tree straight button == "x" i will win i can change the text of button if it got click right by .config – Charls Delante Feb 21 '22 at 05:56

1 Answers1

1

Since the object of tkinter consists of a dictionary, you can get a value by accessing the key value 'text'.

from tkinter import *

app = Tk()
def k():
    if button1["text"] == "1":
        l = Label(app,text="wow")
        l.pack()
    
button1 = Button(app,text="1",command=k)
button1.pack()

app.mainloop()
Desty
  • 328
  • 1
  • 5