【问题标题】:Python: I'm trying to get a text entry box to replace a button with tkinter but the destroy commands don't seem to workPython:我试图让一个文本输入框用 tkinter 替换一个按钮,但销毁命令似乎不起作用
【发布时间】:2022-10-01 09:14:53
【问题描述】:

我是 Python 的一个完整的初学者,我正在尝试用我正在写的东西制作一个交互式项目。

首先,这是我正在做的一个例子。

    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()

我想要发生的是标签文本在按下按钮后发生变化,并且按钮消失,然后被文本输入框替换。问题是我已经对此进行了调查并遇到了 destroy 命令,但由于某种原因,它似乎在这种情况下不起作用。我得到的只是一个 NameError。是因为我告诉它要销毁的按钮仅在函数中定义吗?

    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()

  • 您需要了解变量范围。 button_1button_2start() 函数中的局部变量,您不能在其他函数中访问它们。您需要使它们成为全局变量。

标签: python tkinter tkinter-entry tkinter-button


【解决方案1】:

start() 方法之前移动 button_1_pressedbutton_2_pressed

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() 

输出: 主风带 2 个按钮。

带有 button_1 的主窗口销毁。

button_2 销毁的主窗口。

【讨论】:

    猜你喜欢
    • 2016-01-10
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多