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

尼克·维萨(Nick Vessa)

我正在编写一个应用程序,它具有一个功能,当在列表框中选择“是”时,它将运行放置标签的代码。当我使用.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它也可以在其他函数中定义。

希望它清除您的疑问和错误。

干杯

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我导航回片段时为什么调用onChanged?

当我在OnClientClick中调用Javascript函数时,为什么我的Javascript函数没有被调用

当我尝试从班级外部访问字段时,为什么没有调用我的getter方法?

为什么当我使用 <TouchableWithoutFeedback> 时我的设计消失了?

当我调用类的实例方法时,为什么初始化不显示为方法?

当我们不使用方法隐藏时,为什么不调用父方法?

当我仅调用SonarQube分析时,为什么Gradle构建我的Java项目?

为什么当我调用冠状病毒API时我的应用抛出错误?

当我尝试调用我的函数时,为什么会出现“未定义”?

为什么当我调用函数时我的程序会停止?

为什么当我调用这个函数时我的表没有生成?

当我从单独的viewController中删除CoreData实体时,为什么要调用UITableView方法?

当我尝试在其上调用方法时,为什么UICollectionViewDataSource被释放了?

谁能告诉我为什么当我更改填充时此列表消失了,而另一个却不消失?

当我再次调用Rest API时,为什么会出现403(禁止)错误?

当我单击控制台上的关闭按钮时,为什么不调用析构函数?

为什么当我们调用“任务”变量时,必须将其写为“ task()”

当我从外部类调用它时,为什么会得到ClassNotFoundException?

当我在Thread对象上调用run()时,为什么Java程序会泄漏内存?

为什么当我们调用静态最终变量时不首先执行静态块

gradle-当我执行任务时,为什么还要调用配置阶段

为什么当我调用JsonConvert.DeserializeObject()时将JArray分配给var失败?

当我尝试从成员函数调用构造函数时,IDE为什么会给出警告?

当我从 C# 代码调用导入的 C++ 函数时,为什么会抛出 AccessViolationException?

当我从atexit处理程序调用exit时,为什么没有得到无限循环?

当我尝试调用createTargetMachine()时,为什么LLVM出现段错误?

为什么当我在Android中调用“获取”时屏幕没有唤醒?

为什么当我调用 postInvalidate() 时 OnDraw 没有触发?

PyGObject - 当我调用函数时 GtkSpinner 不旋转 - 为什么?