Alberto Olivieri
Alberto Olivieri

Reputation: 25

Python - Tkinter iterating through a list

I'm stuck with a problem. I'm trying to create a small program that go throughout a list of words, showing the single word in a listbox and pausing at every word unless you hit next, or whatever. I write down code for python and it's a no-brainer.

import random

Deutsch = []

def esercizio():
        rnd = random.sample(range(0, len(Deutsch)), len(Deutsch))
        for i in rnd:
            print(Deutsch[i])
            input()

Of course this doesn't work with TK, and I have no idea of how to do the same, and I don't want to use the after() method (I know that will be the simplest solution). This is my code now:

from tkinter import *
import random

Deutsch = [['viel', 'molta'], ['Glück', 'fortuna'], ['guten', 'buon']]

class Exercise:

    def __init__(self, wlist):
        rnd = random.sample(range(0, len(wlist)), len(wlist))
        self.De = []
        self.It = []
        for i in rnd:
            self.De.append(wlist[i][0])
            self.It.append(wlist[i][1])

    def Start(self, num):
        list1.delete(0, END)
        list1.insert(END, self.De[num])
        print(self.De)

    def Next(self):
        rnd = random.sample(range(1, len(self.De)), len(self.De) - 1)
        for i in rnd:
            list1.delete(0, END)
            list1.insert(END, self.De[i])
            input()

window = Tk()
ex = Exercise(Deutsch)


window.wm_title('Esercizi di Tedesco')
l1 = Label(window, text='Parola Tedesca')
l1.grid(row=1, column=1)

l2 = Label(window, text='Traduzione suggerita')
l2.grid(row=1, column=3)

l3 = Label(window, text='Soluzione')
l3.grid(row=1, column=5)

list1 = Listbox(window, height=1, width=30)
list1.grid(row=3, column=1, rowspan=1, columnspan=1)

list2 = Listbox(window, height=1, width=30)
list2.grid(row=3, column=5, rowspan=1, columnspan=1)

sugg_text = StringVar()
e1 = Entry(window, textvariable=sugg_text)
e1.grid(row=3, column=3)

b1 = Button(window, text="Start", width=12, command=lambda: ex.Start())
b1.grid(row=3, column=6)

b2 = Button(window, text="Solution", width=12)
b2.grid(row=4, column=6)

b3 = Button(window, text="Next", width=12, command=lambda: ex.Next())
b3.grid(row=3, column=7)

b4 = Button(window, text="Close", width=12, command=window.destroy)
b4.grid(row=4, column=7)

window.mainloop()

Upvotes: 0

Views: 1691

Answers (2)

Novel
Novel

Reputation: 13729

If I understand you correctly you want a new random word every time you click Next? Make your De list an iterator, then you can grab the next one with the next() function.

def __init__(self, wlist):
    rnd = random.sample(range(0, len(wlist)), len(wlist))
    self.De = []
    self.It = []
    for i in rnd:
        self.De.append(wlist[i][0])
        self.It.append(wlist[i][1])
    self.De = iter(self.De)
    self.It = iter(self.It)

def Next(self):
    list1.delete(0, END)
    list1.insert(END, next(self.De))

Upvotes: 2

Adam Barnes
Adam Barnes

Reputation: 3203

Your problem is that you're using a "blocking" function, input(), within tkinter's event loop.

tkinter, and all other GUI frameworks (that I know of), maintain a quickly spinning event loop, which keeps the GUI responding. It expects everything it runs to be non-blocking, (basically very fast-completing), because otherwise, the application will hang.

If you want to take input in a GUI program, you will need to add some manner of input to the GUI program, such as a text box, and a "submit" button. Then, when the "submit" button is clicked, the input is read from the text box, and acted upon.

Upvotes: 1

Related Questions