1

I'm a complete beginner at Python and am trying to make an interactive project with what I'm writing.

To start off, here's an example of what I'm doing.

    from tkinter import *

    window = Tk()
    window.title("I am a title")
    window.minsize(width=700, height=300)

    canvas = Canvas(width=900, height=400)
    canvas.grid(column=3,row=3)

    def button_1_pressed():
      print("You pressed button 1!")

    def button_2_pressed():
      print("You pressed button 2!")

    def start():
      label = Label(text="I am a label! \nWill you press a button?")
      label.grid(column=2, row=0)
      button_1 = Button(window, text="Button 1", command=button_1_pressed)
      button_1.grid(column=2, row=1)
      button_2 = Button(window, text="Button 2", command=button_2_pressed)
      button_2.grid(column=2, row=2)

    start()
    window.mainloop()

What I want to happen is for the label text to change after pressing a button and for the buttons to disappear and then be replaced with a text entry box. The problem is that I've looked into this and have come across the destroy command but for some reason, it doesn't appear to work in this instance. All I get is a NameError. Is it because the buttons I'm telling it to destroy are only defined within a function?

    from tkinter import *

    window = Tk()
    window.title("I am a title")
    window.minsize(width=700, height=300)

    canvas = Canvas(width=900, height=400)
    canvas.grid(column=3,row=3)

    def start():
      label = Label(text="I am a label! \nWill you press a button?")
      label.grid(column=2, row=0)
      button_1 = Button(window, text="Button 1", command=button_1_pressed)
      button_1.grid(column=2, row=1)
      button_2 = Button(window, text="Button 2", command=button_2_pressed)
      button_2.grid(column=2, row=2)

    def button_1_pressed():
      print("You pressed button 1!")
      button_1.destroy()

    def button_2_pressed():
      print("You pressed button 2!")
      button_2.destroy()

    start()
    window.mainloop()

ZionWario
  • 11
  • 2
  • 2
    You need to learn about variable scopes. `button_1` and `button_2` are local variables in the `start()` function, you can't access them in the other functions. You need to make them global variables. – Barmar Sep 30 '22 at 21:59

1 Answers1

0

Move button_1_pressed and button_2_pressed before start() method.

from tkinter import *

window = Tk()
window.title("I am a title")
window.minsize(width=700, height=300)

canvas = Canvas(width=900, height=400)
canvas.grid(column=3,row=3)

def button_1_pressed():
    print("You pressed button 1!")
    button_1.destroy()

def button_2_pressed():
    print("You pressed button 2!")
    button_2.destroy()

def start():
    label = Label(text="I am a label! \nWill you press a button?")
    label.grid(column=2, row=0)
    
button_1 = Button(window, text="Button 1", command=button_1_pressed)
button_1.grid(column=2, row=1)
button_2 = Button(window, text="Button 2", command=button_2_pressed)
button_2.grid(column=2, row=2)

start()
window.mainloop() 

Output: Main wind with 2 buttons.

enter image description here

Main window with button_1 destroy.

enter image description here

Main window with button_2 destroy.

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19