-1

I am having a trouble changing text in python even though though that I saw a lot of use it

My code:

from tkinter import *
window = Tk()

def change_text():
    btn.config(text = "second text")

btn = Button(window ,text= "first text" , command= change_text).pack()

window.mainloop()

martineau
  • 119,623
  • 25
  • 170
  • 301
Tiger Lion
  • 35
  • 1
  • 5

2 Answers2

4

You assigned the return value of .pack() to btn, and pack doesn't return the widget it was called on (it returns None implicitly, since it has no useful return value). Just split up the creation of the button from the packing:

from tkinter import *
window = Tk()

def change_text():
    btn.config(text="second text")


btn = Button(window, text="first text", command=change_text)
btn.pack()

window.mainloop()
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
2

your code is good but it is not good to make .pack() in the same line


from tkinter import *
window = Tk()

def change_text():
    btn.config(text = "second text")


btn = Button(window ,text= "first text" , command= change_text )
btn.pack()





window.mainloop()

it works just will

Ali Saad
  • 120
  • 8