简体   繁体   English

如何摆脱TkInter中的标签?

[英]How do I get rid of a label in TkInter?

I have looked for ways to do this but most of them are for instances that don't help me. 我已经找到了方法来做到这一点,但大多数都是为了帮助我的实例。

Here is my code - 这是我的代码 -

import os
import time
import tkinter
from tkinter import *

root = Tk()
root.title('Interpreter')
Label(text='What is your name?').pack(side=TOP,padx=10,pady=10)

entry = Entry(root, width=30)
entry.pack(side=TOP,padx=10,pady=10)

def onOkay():
    print = str(entry.get())
    myName = Label(text='Your name is '+print+'.').pack(side=BOTTOM,padx=15,pady=10)
    myName


def onClose():
    #Nothing here yet

Button(root, text='OK', command=onOkay).pack(side=LEFT,padx=5,pady=5)
Button(root, text='CLOSE', command=onClose).pack(side= RIGHT,padx=5,pady=5)

root.mainloop()

You can use the pack_forget() method on a widget to hide it. 您可以在窗口小部件上使用pack_forget()方法来隐藏它。

However, you should change a couple of things in your onOkay() function: 但是,您应该在onOkay()函数中更改一些内容:

def onOkay():
    global myName #make myName a global so it's accessible in other functions
    name = entry.get() #print shouldn't be a variable name. Also, .get() returns a string, so str() is redundant
    myName = Label(root, text='Your name is '+name+'.')
    myName.pack(side=BOTTOM,padx=15,pady=10) #put this on a new line so myName is a valid variable

And onClose: 并且关闭:

def onClose():
    myName.pack_forget()

Edit: It's unclear if this is what you want your program to do (ie, forget the myName label when the Close button is pressed), but hopefully you can work it out from here. 编辑:目前还不清楚这是否是您希望程序执行的操作(即,在按下“关闭”按钮时忘记myName标签),但希望您可以从此处进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM