每次我按下一个按钮,我都试图清除标签的内容。我不知道该用什么函数。在
Every time I use the button b1 or NEXT it reads the next word from the database, and its meaning. The only problem I have is that the previous contents of the labels l1 and l2 are not being erased and instead overwritten.
The disp_m() method- displays the meaning and the tki_met() calls the next word.
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()
至于我,你的代码有点混乱,所以我几乎做了新版本。
也许这不是解决SO问题的最佳方法
但它也展示了如何在
Label
中更改文本。在我做了一个小的数据库来测试它,它成功了。在
一开始我创建没有文本的标签,我使用self。(self.l1,self.l2)以其他方法访问它们。当我按下按钮,然后功能更改标签
self.l1['text'] = self.word_str
或self.l2['text'] = self.meaning_str
中的文本相关问题 更多 >
编程相关推荐