TKinter - 明文小部件 [英] TKinter - Clear text widget

查看:40
本文介绍了TKinter - 明文小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按下按钮时将一个从 1 到 25 的随机数输入到文本框中.每次按下按钮时,我都希望在存储新数字之前清空文本框 - 但将 self.outputbox.delete("1.0") 放入按钮命令只会删除第一个字符.我尝试用 2.0 之类的明显内容替换 1.0,但它从未删除这两个字符.目前,我只是通过调用 self.outputbox.delete("1.0") 两次来做一个非常脏的解决方案 - 但我想知道如何删除 整个文本框的内容.

I am inputting a random number from 1 to 25 into a text box on button press. Every time i press the button, i want the text box to be emptied before the new number is stored - but putting self.outputbox.delete("1.0") into the button command only deletes the first character. i tried replacing 1.0 by something obvious like 2.0, but it never deleted both characters. At the moment, i am doing a very dirty solution by just calling self.outputbox.delete("1.0") twice - but i want to know how to delete the entire content of the text box.

这是部分:

   def change_button_color(self):
       randomcolor = self.get_random_color()
       randombutton = self.random_button()
       for z in range(0,1):
          self.buttons['button{}'.format(randombutton)].config(bg=randomcolor)
          for i in range(0,40):
              self.outputbox.delete("1.0")
          self.outputbox.insert("1.0","Button" + str(randombutton) + " " + "has the color" + " " + randomcolor)

如您所见,我目前有一个相当脏的解决方案来清除盒子

as you can see, i currently have a rather dirty solution to clear the box

推荐答案

这样做

self.outputbox.delete("1.0", self.END)

它将清除所有文本widget

如果您将 tkinter 作为 tk 导入,请执行此操作

And if you imported tkinter as tk do this

self.outputbox.delete("1.0", self.tk.END)

这篇关于TKinter - 明文小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆