【问题标题】:Tkinter Label OverwriteTkinter 标签覆盖
【发布时间】:2015-01-29 05:35:15
【问题描述】:

我正在使用 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)

【问题讨论】:

    标签: python python-2.7 tkinter label


    【解决方案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?")
    

    【讨论】:

      【解决方案2】:

      我遇到了完全相同的问题,而且非常烦人——同时尝试grid_destroy()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)
      

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

      【讨论】:

      • 请使用格式化工具正确编辑和格式化您的答案。句子中的代码格式为code 非常重要的单词要粗体,次要的单词要斜体必要时也可以使用列表
      猜你喜欢
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多