简体   繁体   English

如何清除Tkinter上“文本”小部件中的文本?

[英]how can I clear text from a Text widget on Tkinter?

So I made a statusbar on Tkinter and I want there to show up some values. 所以我在Tkinter上做了一个状态栏,我想在那里显示一些值。 Sometimes that value changes, so I need the statusbar to change to. 有时该值会更改,因此我需要状态栏更改为。 When I run my program the value shows just fine, but when I change the value and I insert it, the old value stays and the new one is printed next to the old one. 当我运行我的程序时,该值显示就很好,但是当我更改该值并插入它时,旧的值会保留,而新的值会打印在旧的值旁边。

My question is how can I first delete the first one and then add the other one? 我的问题是如何首先删除第一个,然后添加另一个?

from tkinter import *
class lol:

   def __init__(self, window):
       frame = Frame(window)
       frame.grid()

       self.statusbarTotal = Text(window, width= 8, height= 1, bd=1, relief= SUNKEN)
       self.statusbarTotal.grid(row=13, column=0, sticky=W)

       self.presetButton1 = Button(window, text=" 10 ", command=self.preset10)
       self.presetButton1.grid(row=2, column=0)

       self.presetButton2 = Button(window, text=" 30 ", command=self.preset30)
       self.presetButton2.grid(row=2, column=1)


   def preset10(self):
          global waitingTime
          waitingTime = 600
          self.statusbarTotal.insert(13.0, int(waitingTime/60))
          print(waitingTime)

   def preset30(self):
          global waitingTime
          waitingTime = 1800
          self.statusbarTotal.insert(13.0, int(waitingTime/60))
          print(waitingTime)

tk = Tk()
b = lol(tk)
tk.mainloop()

Aside from your indention issues. 除了缩进问题。 You need to change: 您需要更改:

def preset10(self):
    global waitingTime
    waitingTime = 600
    self.statusbarTotal.insert(13.0, int(waitingTime/60))
    print(waitingTime)

def preset30(self):
    global waitingTime
    waitingTime = 1800
    self.statusbarTotal.insert(13.0, int(waitingTime/60))
    print(waitingTime)

To: 至:

def preset10(self):
    global waitingTime
    waitingTime = 600
    self.statusbarTotal.delete(1.0, END)
    self.statusbarTotal.insert(13.0, int(waitingTime/60))
    print(waitingTime)

def preset30(self):
    global waitingTime
    waitingTime = 1800
    self.statusbarTotal.delete(1.0, END)
    self.statusbarTotal.insert(13.0, int(waitingTime/60))
    print(waitingTime)

However your program shows all the data on one line so I am not sure why you are trying to insert values on the 13th line of the text box. 但是,您的程序将所有数据显示在一行上,因此我不确定为什么要在文本框的第13行上插入值。 If you can provide context or a better example I can provide a better answer. 如果可以提供上下文或更好的示例,我可以提供更好的答案。

However you should really get rid of your global variables all together as this is considered bad programing to use. 但是,您应该真正摆脱global变量,因为这被认为是不好的编程方法。

Instead your code should use class attributes. 相反,您的代码应使用类属性。 Something like this: 像这样:

from tkinter import *


class lol:

    def __init__(self, window):
        frame = Frame(window)
        frame.grid()
        # uss self. to make a variable name a class attribute
        self.waitingTime = 0

        self.statusbarTotal = Text(window, width= 8, height= 1, bd=1, relief= SUNKEN)
        self.statusbarTotal.grid(row=13, column=0, sticky=W)

        self.presetButton1 = Button(window, text=" 10 ", command=self.preset10)
        self.presetButton1.grid(row=2, column=0)

        self.presetButton2 = Button(window, text=" 30 ", command=self.preset30)
        self.presetButton2.grid(row=2, column=1)


    def preset10(self):
        self.waitingTime = 600
        self.statusbarTotal.delete(1.0, END)
        self.statusbarTotal.insert(13.0, int(self.waitingTime/60))
        print(self.waitingTime)

    def preset30(self):
        self.waitingTime = 1800
        self.statusbarTotal.delete(1.0, END)
        self.statusbarTotal.insert(13.0, int(self.waitingTime/60))
        print(self.waitingTime)

tk = Tk()
b = lol(tk)
tk.mainloop()

Several possibilities. 几种可能性。

The first is that insert will, well, insert the new text into the existing text. 第一个是insert会很好地新文本插入到现有文本中。 So you have to use delete to remove the part that you don't want anymore. 因此,您必须使用delete删除不再需要的部分。

Another solution, which is simpler I think, is don't use a Text widget but instead use Label , which allows you to set and replace the text it displays all at once. 我认为更简单的另一种解决方案是不使用Text小部件,而使用Label ,它允许您设置和替换一次显示的所有text You can't manually edit the text in a Label widget but I suppose for a status bar that is not what you want anyway. 您不能在Label小部件中手动编辑文本,但是我想使用的状态栏不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM