Tkinter的标签覆盖(Tkinter Label Overwrite)

2019-10-28 10:40发布

我使用的Tkinter做一个小项目。 长话短说,我希望有一个标签,产生创作上的一些东西。

后来,我想在同一个地方,但不同的文本打印标签。 我不能为我的生活弄清楚如何删除以前的文字。

我在这个例子风与是文本“这是否甚至工作”字样的“我要的是一个非常长期的,烦人的句子测试”顶

谢谢!

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)

Answer 1:

reveal ,你要创建一个新的Label在同一地点一张标签,你的网格。 你想要做的是使用这样的:

    # 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?")


Answer 2:

我有相同的问题,这是令人难以置信的恼人的-试图既grid_destroy()grid_forget() 但是都没有成功。 什么工作对我来说是调用标签2次,第一次用很多的空间,第二个与我其实是想展示。 通过这种方式,标签内容被覆盖的空间,而不是旧的内容。 我知道这是不是要做到这一点的最好办法,但它得到我,我想不头疼的结果:

    NumAssetsLabel = tk.Label(self, text="Num of Assets:                                       ", font=('Helvetica', 10))
    NumAssetsLabel.grid(row=9, column=3, pady=20, padx=10, sticky="w", columnspan=2)
    NumAssetsLabel = tk.Label(self, text="Num of Assets: %s"%runCheck, font=('Helvetica', 10))
    NumAssetsLabel.grid(row=9, column=3, pady=20, padx=10, sticky="w", columnspan=2)

请注意,我有columnspan=2 ,以确保空格数量不影响电网。



文章来源: Tkinter Label Overwrite