Tkinter Label Overwrite

白昼怎懂夜的黑 提交于 2019-12-25 01:13:18

问题


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"

Thanks!

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)

Bunch of MySQL stuff deleted

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

回答1:


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



回答2:


I had the exact same issue, and it was incredibly annoying--trying both grid_destroy() and grid_forget(). Neither worked. What worked for me was to call the label 2 times, the first time with many spaces, and the second with what I actually wanted to show. This way, the label content is overwriting spaces, not old content. I know this is not the best way to do this, but it gets me the result I want without a headache:

    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)

Note that I have columnspan=2, to make sure that the number of spaces does not impact the grid.



来源:https://stackoverflow.com/questions/27224570/tkinter-label-overwrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!