代码之家  ›  专栏  ›  技术社区  ›  Rahul

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

  •  0
  • Rahul  · 技术社区  · 7 年前

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

    每次我用这个按钮 b1 下一个 它从数据库中读取下一个单词及其含义。我唯一的问题是标签之前的内容 l1和l2 不会被擦除,而是被覆盖。

    这个 disp\u 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()
    
    1 回复  |  直到 4 年前
        1
  •  0
  •   furas    7 年前

    至于我,你的代码有点乱,所以我几乎做了新版本。
    也许这不是解决问题的最好方法
    但它展示了如何在 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_str self.l2['text'] = self.meaning_str