Python中循环问题的Tkinter按钮
我正在尝试构建一个简单的GUI,其中有一个句子列表,有一个带有tkinter文本的for循环,在其中显示这些句子,我希望循环仅在单击按钮时迭代并显示列表中的下一个句子,我如何实现这一点,谢谢。我尝试了wait_变量,但它不起作用Python中循环问题的Tkinter按钮,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在尝试构建一个简单的GUI,其中有一个句子列表,有一个带有tkinter文本的for循环,在其中显示这些句子,我希望循环仅在单击按钮时迭代并显示列表中的下一个句子,我如何实现这一点,谢谢。我尝试了wait_变量,但它不起作用 var = IntVar() for entry in input_texts: scroll = Scrollbar(canvas) display = Text(canvas, height=2, width=110) display.ins
var = IntVar()
for entry in input_texts:
scroll = Scrollbar(canvas)
display = Text(canvas, height=2, width=110)
display.insert(INSERT, entry)
display.grid(row=1, sticky='w')
scroll.grid(row=1, column=4)
display.config(yscrollcommand=scroll.set)
scroll.config(command=display.yview)
confirm = Button(canvas, text=" NEXT ", command=pause)
confirm.grid(row=4, sticky='w')
confirm.wait_variable(var)
var.set(0)
canvas.resizable(width=False, height=False)
canvas.mainloop()
有很多方法可以做到这一点。下面是一个相当简单的方法
import tkinter as tk
class Sentence:
def __init__(self, master):
self.text = tk.Text(master)
self.scrolly = tk.Scrollbar(master, command=self.text.yview)
self.scrolly.grid(row=0, column=1, sticky='nsw')
self.text.grid(row=0, column=0, sticky='news')
self.text['yscrollcommand'] = self.scrolly.set
# Set the command attribute of the button to call a method that
# inserts the next line into the text widget.
self.button = tk.Button(master, text='Next', command=self.insertLine)
self.button.grid(row=1, pady=10, column=0, sticky='n')
self.data = ['Here is an example', 'This should be second', '3rd', 'and so on...']
self.data.reverse()
def insertLine(self):
if len(self.data):
# Pull the first line from the list and display it
line = self.data.pop()
self.text.insert(tk.END, line + '\n')
self.text.see(tk.END)
else:
print("No more data")
if __name__ == '__main__':
root = tk.Tk()
sentence = Sentence(root)
root.mainloop()
你试过什么?显示一些代码。添加代码,“确认”是这里的相关按钮,输入文本是句子列表。非常感谢,我真傻,没有这样想。我仍然有一个小问题,虽然文本被插入到文本框,向下滚动的动作不会自动发生,有可能吗?顺便说一下,即使在插入新文本时删除以前的文本也可以,而且可能更简单。如果您不想删除顶部的信息,而是滚动,我在insertLine中添加了一行,它会自动为您执行此操作。