【问题标题】:python/tkinter add the individual letters of a word to a label on button presspython/tkinter 在按钮按下时将单词的单个字母添加到标签中
【发布时间】:2018-04-29 09:08:12
【问题描述】:
from tkinter import *

# Create a window
spell_window = Tk()

# Give the window a title
spell_window.title('Spell Table')


table_Var = StringVar()

## table = ['T', 'a', 'b', 'l', 'e'] ## I think it needs to move through a list???

def spell_table():
    s_table = monty_Var.get()
    s_table += 'T' #Currently adds a 'T' each time the button is pressed
    monty_Var.set(s_table)



the_label = Label(spell_window, width = 10, textvariable = table_Var,
                  font = ('Arial', 30), bg = 'red')

the_button = Button(spell_window, text = 'Next letter', command = spell_table)

the_label.pack(padx = 0, pady = 0)

the_button.pack(padx = 40, pady = 0)

所以基本上我已经创建了标签和按钮,并且需要通过按下一个字母按钮来拼出单词 Table。只是不知道如何让它在表格列表中移动并将它们添加到标签中。

【问题讨论】:

    标签: python button tkinter label


    【解决方案1】:

    这里是固定代码:

    from tkinter import *
    
    spell_window = Tk()
    
    spell_window.title('Spell Table')
    
    table = ['T', 'a', 'b', 'l', 'e']
    count = 1
    
    def spell_table():
       global count, table
       the_label.config(text=table[:count])
    
       if count < len(table):
          count += 1
    
    
    
    the_label = Label(spell_window, width = 10, text = "", font = ('Arial', 30), bg = 'red')
    
    the_button = Button(spell_window, text = 'Next letter', command = spell_table)
    
    the_label.pack(padx = 0, pady = 0)
    
    the_button.pack(padx = 40, pady = 0)
    
    spell_window.mainloop()
    

    我删除了无用的变量并添加了一个随着每次点击而增加的“计数”变量。是的,你确实需要一个列表,一个元组也可以。我从

    更改了“the_label”的文本
    'textvariable = ' to 'text = ""'.
    

    我还根据“计数”对列表进行了切片。 [:count] = count 的索引值之前的任何内容。还有 if 语句只是为了提高效率,因为我们不需要在变量超过“表”列表的索引值后为其分配更多空间。你也错过了一个

    spell_window.mainloop()
    

    最后。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      相关资源
      最近更新 更多