1

I'm trying to build a simple GUI where there's a list of sentences, and there's a for loop with tkinter Text where these sentences are being displayed, I want the loop to iterate and show the next sentence from the list only when the button is clicked, how can I achieve this, thanks. I tried wait_variable but it's not working.

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()
2
  • What you have tried ? Show some code. Commented Apr 26, 2018 at 17:56
  • added code, 'confirm' is the concerned button here, and input_texts is the list of sentences. Commented Apr 26, 2018 at 17:59

1 Answer 1

1

There are many ways to do this. Here is a fairly simple way to do it.

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, stupid of me not to think it this way. I'm still having one minor issue though, as texts are being inserted into the textbox, the scrolling down action is not happening automatically, is it possible to do that? By the way even deleting the previous texts while the new one is being inserted will be fine as well, and probably simpler.
If you'd rather not delete the information at the top, but scroll, I've added the line in insertLine that will do that automatically for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.