如何在每次按下按钮时清除tkinter中标签的内容

2024-05-29 11:05:00 发布

您现在位置:Python中文网/ 问答频道 /正文

每次我按下一个按钮,我都试图清除标签的内容。我不知道该用什么函数。在

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()

Tags: andthetextfromimportselfdefcolumn
1条回答
网友
1楼 · 发布于 2024-05-29 11:05:00

至于我,你的代码有点混乱,所以我几乎做了新版本。
也许这不是解决SO问题的最佳方法
但它也展示了如何在Label中更改文本。在

我做了一个小的数据库来测试它,它成功了。在

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()

一开始我创建没有文本的标签,我使用self。(self.l1,self.l2)以其他方法访问它们。当我按下按钮,然后功能更改标签self.l1['text'] = self.word_strself.l2['text'] = self.meaning_str中的文本

相关问题 更多 >

    热门问题