Tkinter标签覆盖

时间:2014-12-01 08:45:23

标签: python python-2.7 tkinter label

我使用Tkinter做一个小项目。长话短说,我想要一个标签来创造一些东西。

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

在这个例子中我最终得到的是文字"这是否有效?"打印在"所有我想要的是一个非常长和令人生气的测试和#34;

谢谢!

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)

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我遇到了完全相同的问题,这非常令人讨厌 - 同时尝试arrayOfClasses: AbstractSection[] = [...]; grid_destroy()。都没有奏效。对我有用的是将标签称为2次,第一次使用许多空格,第二次使用我实际想要显示的内容。这样,标签内容就会覆盖空格,旧内容。我知道这不是最好的方法,但它让我得到了我想要的结果,而不是头疼:

grid_forget()

请注意,我有 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) ,以确保空格数不会影响网格。