python GUI按钮处理标签文本
嘿 我想用Python编写一个GUI,将提示词(从列表中)与条目进行比较。这将是可能的几次,所以我想做一个按钮,删除条目和标签,然后提示列表中的下一个单词。 因此,点击按钮应操作标签上显示的文本。 我该怎么做 我的代码摘录是:python GUI按钮处理标签文本,python,user-interface,button,tkinter,label,Python,User Interface,Button,Tkinter,Label,嘿 我想用Python编写一个GUI,将提示词(从列表中)与条目进行比较。这将是可能的几次,所以我想做一个按钮,删除条目和标签,然后提示列表中的下一个单词。 因此,点击按钮应操作标签上显示的文本。 我该怎么做 我的代码摘录是: from Tkinter import * i = 0 vocablist = ['one', 'two', 'three'] np.random.shuffle(vocablist) vocab = vocablist[i] (...) class simplea
from Tkinter import *
i = 0
vocablist = ['one', 'two', 'three']
np.random.shuffle(vocablist)
vocab = vocablist[i]
(...)
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
(...)
self.label = Tkinter.Label(self, text=vocab,anchor="w")
self.label.grid(column=1,row=0)
def clear_text():
global i
self.entry.delete(0, 'end')
i += 1 # don't know if this works!
self.label.insert(0, vocab) # this does not work!
button_next = Tkinter.Button(self,text=u"next", command=clear_text)
button_next.grid(column=1,row=1)
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('rehearse vocabulary: translate!')
app.mainloop()
我也试过了
self.label.config(text=vocab)
而不是.insert函数
我想知道是命令出了问题,还是更新计数器然后自动将变量“vocab”更新到列表中的下一个元素不起作用
谢谢你的帮助 尝试使用
小部件['attribute']='value'
。请执行以下操作:
def action(event=None): # event in case you bind to a keypress
entry.delete('0', END) # clear the entry
label['text'] = "" # set label's widget text to ""
next_word()
其中,
entry
是您的输入小部件,label
是您的标签,next\u word()
提示输入下一个单词。self.label.config(text=vocab)
是正确的解决方案。你说你试过了,但没有说你试过的时候发生了什么。有可能整个事情都不起作用,因为import语句是来自Tkinter import*的,但随后可以使用Tkinter.Widgetname
访问它。谢谢你的回答!我找到了一个使用self.label.config(text=vocablist[I])而不是vocab的解决方案。通过这个,计数器工作。您分配了vocab=vocablist[i]
,所以我不明白为什么text=vocab
不工作。。。