【问题标题】:How do I get rid of a label in TkInter?如何摆脱 TkInter 中的标签?
【发布时间】:2014-03-18 16:23:38
【问题描述】:

我一直在寻找方法来做到这一点,但大多数都是针对对我没有帮助的情况。

这是我的代码 -

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

【问题讨论】:

标签: python tkinter label


【解决方案1】:

您可以在小部件上使用pack_forget() 方法来隐藏它。

但是,您应该在 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

然后关闭:

def onClose():
    myName.pack_forget()

编辑:目前尚不清楚这是否是您希望程序执行的操作(即,按下关闭按钮时忘记 myName 标签),但希望您可以从这里解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多