Tkinter标签覆盖 [英] Tkinter Label Overwrite

查看:564
本文介绍了Tkinter标签覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tkinter进行一个小项目.长话短说,我希望标签在创作时产生一些东西.

I'm using Tkinter to do a small project. Long story short, I want a label to generate some stuff on creation.

稍后,我想在同一位置打印标签,但使用不同的文本. 我一辈子都无法弄清楚如何删除以前的文字.

Later on, I want to print a label in the same spot but with different text. I cannot for the life of me figure out how to remove the previous text.

在此示例中,我想想的是在我想要的很长且令人讨厌的测试句子"上方印刷的文本此功能是否还能工作"

What I wind up with in this example is the text "Does this even work" printed on top of "ALL I WANT IS A VERY LONG AND ANNOYING SENTENCE FOR TESTING"

谢谢!

from Tkinter import *
import pygal
class Application(Frame) :
    def __init__(self, root) :
        Frame.__init__(self, root)
        self.root = root
        self.grid()
        self.create_widgets()
    def create_widgets(self) :
        queryLabel = Label(self, text = "Enter your query :")
        queryLabel.grid(row=0, column=0, sticky=W)
        self.userQuery = StringVar()
        qEntry = Entry(self, textvariable=self.userQuery)
        qEntry.grid(row=0, column=1, sticky=W)
        queryTypeLabel = Label(self,text="Selecting query type: ")
        queryTypeLabel.grid(row=2, column=0, sticky=W)
        self.searchType = StringVar()
        authorButton = Radiobutton( self, text="Author", variable=self.searchType, value="author")
        authorButton.grid(row=3,column=0,sticky=W)
        memberButton = Radiobutton( self, text="PC Member", variable=self.searchType, value="pcmember")
        memberButton.grid(row=3,column=1,sticky=W)
        affilButton =  Radiobutton( self, text="Affiliation", variable=self.searchType, value="affiliation")
        affilButton.grid(row=3,column=2,sticky=W)
        self.conference = BooleanVar()
        confCheck = Checkbutton(self, text="Select Conference?", variable = self.conference)
        confCheck.grid(row=4,column=0,sticky=W)
        self.conferenceName = StringVar()
        self.conferenceName.set('No Preference')
        confDropDown = OptionMenu(self, self.conferenceName, *conferenceOptions)
        confDropDown.grid(row=4,column=1,sticky=W)
        submit_button = Button( self, text="Submit", command = self.reveal)
        submit_button.grid(row=5, column = 0, sticky = W)
        #self.graphics_button = Button( self, text="I want Statistics!", command=self.graphics)
        #self.graphics_button.grid(row=5, column = 1, sticky= W)
        label1 = Label(root, text="ALL I WANT IS A VERY LONG AND ANNOYING SENTENCE FOR TESTING")
        label1.grid(row=6, column=0, columnspan=5, sticky=W)
    def reveal(self) :
        #root.submit_button.grid_destroy()
        self.label1 = Label(root, text="Does this even work?")
        self.label1.grid(row=6, column=0, columnspan=5, sticky=W)

一堆MySQL资料已删除

### Launch the UI
root = Tk()
root.geometry("800x800+300+300")
app = Application(root)

推荐答案

reveal中,您将创建一个新的Label,并在与上一个标签相同的位置进行网格化.您想要做的是使用此:

In reveal, you're creating a new Label which you grid in the same place as the previous label. What you want to do is use this:

    # Make label1 an attribute of self to be able to reference it in reveal
    self.label1 = Label(root, text="ALL I WANT IS A VERY LONG AND ANNOYING SENTENCE FOR TESTING")
    self.label1.grid(row=6, column=0, columnspan=5, sticky=W)
def reveal(self) :
    #root.submit_button.grid_destroy()
    # Do not create a new Label, but change the existing one
    self.label1.config(text="Does this even work?")

这篇关于Tkinter标签覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆