[英]How to erase the contents of a label in tkinter everytime I press a button
I am trying to clear the contents of a label each time I press a button. 每次按下按钮时,我都试图清除标签的内容。 I don't know what function to use. 我不知道要使用什么功能。
Every time I use the button b1 or NEXT it reads the next word from the database, and its meaning. 每次我使用按钮b1或NEXT时,它都会从数据库中读取下一个单词及其含义。 The only problem I have is that the previous contents of the labels l1 and l2 are not being erased and instead overwritten. 我唯一的问题是标签l1和l2的先前内容没有被擦除,而是被覆盖了。
The disp_m() method- displays the meaning and the tki_met() calls the next word. disp_m()方法显示含义,而tki_met()调用下一个单词。
from tkinter import *
from random import *
import sqlite3
import sys
class Appx(Tk):
word_str=''
meaning_str=''
rand_mem=int(0)
def start(self):
self.title("Vocabulary")
self.main_app()
self.mainloop()
def main_app(self):
conn = sqlite3.connect("database_word.db")
cur = conn.cursor()
count = cur.execute("select count(word) from words;")
rowcount = cur.fetchone()[0]
x = int(randint(1, rowcount))
if x==self.rand_mem:
x=int(randint(1, rowcount))
conn = sqlite3.connect("database_word.db")
cursor = conn.execute("select * from words;")
for row in cursor:
if row[0] == x:
self.word_str = row[1]
self.meaning_str = row[2]
self.rand_mem=x
l1 = Label(self,text=self.word_str).grid(row=2, column=2)
b2=Button(self, text="MEANING", command=self.disp_m).grid(row=4, column=2)
self.tki_met()
b3=Button(self, text="EXIT", command=self.ex).grid(row=4, column=3)
def tki_met(self): #displays next word
b1 = Button(self, text="NEXT", command=self.main_app).grid(row=4, column=1)
def disp_m(self): #displays meaning
l2 = Label(self, text=self.meaning_str).grid(row=3, column=2)
def ex(self):
sys.exit()
wx=Appx()
wx.start()
As for me there is mess in your code so I made almost new version. 至于我,您的代码中有乱七八糟的东西,所以我做了几乎新的版本。
Maybe it is not the best method to resolve problems on SO 也许这不是解决SO问题的最佳方法
but it shows how to change text in Label
too. 但它也显示了如何在Label
更改文本。
I made small database to test it and it worked. 我制作了一个小型数据库对其进行了测试,并且可以正常工作。
import tkinter as tk
from random import randint
import sqlite3
class Appx(tk.Tk):
def __init__(self):
super().__init__()
self.title("Vocabulary")
self.word_str = ''
self.meaning_str = ''
self.random_mem = 0
self.create_widgets()
self.db_connect()
self.display_next_word()
def run(self):
self.mainloop()
def create_widgets(self):
self.l1 = tk.Label(self)
self.l1.grid(row=2, column=2)
self.l2 = tk.Label(self)
self.l2.grid(row=3, column=2)
b1 = tk.Button(self, text="NEXT", command=self.display_next_word)
b1.grid(row=4, column=1)
b2 = tk.Button(self, text="MEANING", command=self.display_meaning)
b2.grid(row=4, column=2)
b3 = tk.Button(self, text="EXIT", command=self.destroy)
b3.grid(row=4, column=3)
def db_connect(self):
self.conn = sqlite3.connect("database_word.db")
self.cursor = self.conn.cursor()
self.cursor.execute("SELECT count(word) FROM words;")
self.rowcount = self.cursor.fetchone()[0]
def db_get_word(self):
x = randint(1, self.rowcount)
while x == self.random_mem:
x = randint(1, self.rowcount)
self.random_mem = x
# use SQL to get one row
self.cursor.execute("SELECT * FROM words WHERE id = ?;", (self.random_mem,))
row = self.cursor.fetchone()
self.word_str = row[1]
self.meaning_str = row[2]
#TODO: maybe you should use SQL to get random row
# SELECT * FROM words ORDER BY RANDOM() LIMIT 1;
# or
# SELECT * FROM words WHERE id <> ? ORDER BY RANDOM() LIMIT 1; , random_mem
def display_next_word(self):
self.db_get_word()
self.l1['text'] = self.word_str
def display_meaning(self):
self.l2['text'] = self.meaning_str
wx = Appx()
wx.run()
At start I create labels without text and I use self. 开始时,我创建没有文本的标签,然后使用self。 (self.l1, self.l2) to have access to them in other methods. (self.l1,self.l2)以其他方式访问它们。 When I press button then function change texts in label self.l1['text'] = self.word_str
or self.l2['text'] = self.meaning_str
当我按下按钮时,函数会更改标签self.l1['text'] = self.word_str
或self.l2['text'] = self.meaning_str
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.