每次按下按钮

时间:2017-11-24 18:15:18

标签: python tkinter label

每次按下按钮,我都会尝试清除标签的内容。我不知道使用什么功能。

  

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

     

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

1 个答案:

答案 0 :(得分:0)

至于我,你的代码很乱,所以我做了几乎全新的版本 也许这不是解决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.l1,self.l2)可以在其他方法中访问它们。当我按下按钮然后功能更改标签self.l1['text'] = self.word_strself.l2['text'] = self.meaning_str

中的文本