当我调用.place_forget()方法时,为什么我的Label没有消失



我正在编写一个应用程序,它有一个函数,当"是";在ListBox中选择,则它运行放置标签的代码。当我使用.place_forget((方法甚至.place((方法将其移出屏幕时,我得到了两个版本的Label,或者它根本不移动,我很困惑为什么。


#dropdown event handling
def selected(event):
entrydisplay = clicked.get()
if 'Yes' in entrydisplay:
global extrainfo
extrainfo = tk.Entry(root, width=32)
extrainfo.place(x=312,y=201)
extra_label = tk.Label(root, text='If yes, what team and level of FIRST?', width=28, height=1, bg='red3', fg='black')
extra_label.place(x=110, y=200)
interest_label.place(x=130,y=230)
interest_mechanical.place(x=311, y=228)
interest_electrical.place(x=311, y=248)
interest_marketing.place(x=311, y=268)
interest_design.place(x=311, y=288)
interest_programming.place(x=311, y=308)
interest_chairmans.place(x=311, y=328)
interest_advocacy.place(x=311, y=348)
interest_leadership.place(x=311, y=368)
interest_video_production.place(x=311, y=388)
interest_social_media.place(x=311, y=408)
other_checkbox.place(x=311, y=428)
check_box_move = "0"
if 'No' in entrydisplay:
check_box_move = "1"
def Clear():
name.delete(0, END)
email.delete(0, END)
clicked.set(options[2])
grade.delete(0, END)
interest_mechanical.deselect()
interest_electrical.deselect()
interest_marketing.deselect()
interest_design.deselect()
interest_programming.deselect()
interest_chairmans.deselect()
interest_advocacy.deselect()
interest_leadership.deselect()
interest_social_media.deselect()
interest_video_production.deselect()
other_checkbox.deselect()
extrainfo.delete(0, END)
other_entry.delete(0,END)
other_entry.place(x=798, y=400)
extrainfo.place(x=798, y=201)
extra_label.destroy()

这是我写的一个版本,你可以复制我的问题:

from tkinter import *
import tkinter as tk
root = Tk()
root.geometry("400x400+0+0")
root.title("test")

def selected(event):
global test_label
test_label = tk.Label(root, text='TEST')
test_label.place(x=20,y=20)
def Delete():
test_label.destroy
#DROPDOWN BASE CODE
options = [
'Yes',
'No',
'Select'
]
clicked = StringVar()
clicked.set(options[2])
#ENDS ABOVE
Dropdown = tk.OptionMenu(root, clicked, *options, command=selected)
Dropdown.place(x=311, y=165)
delete_button = tk.Button(root, text='Click to delete text', command=Delete)
delete_button.place(x=100,y=100)
root.mainloop()

这个问题与全局和本地作用域非常相关。当你在函数内部定义一个变量时,它只在该函数中定义,为了能够被其他函数发现,你必须使它在全局范围内可用。简单地说,global extra_label也可以在其他函数中定义。

希望它能消除你的疑虑和错误。

干杯

如果您根本不想要该标签,则可以使用destroy()方法从应用程序中永久删除该标签。此外,你应该指定你正在构建的应用程序类型,以使你的问题更加清晰。

最新更新