简体   繁体   English

Python-Tkinter遍历列表

[英]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. 我写下了python的代码,这很容易。

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). 当然,这不适用于TK,而且我也不知道该怎么做,也不想使用after()方法(我知道这是最简单的解决方案)。 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()

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. 将您的De列表设为迭代器,然后可以使用next()函数获取下一个。

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))

Your problem is that you're using a "blocking" function, input() , within tkinter's event loop. 您的问题是您在tkinter的事件循环中使用了“阻塞”函数input()

tkinter, and all other GUI frameworks (that I know of), maintain a quickly spinning event loop, which keeps the GUI responding. tkinter和所有其他GUI框架(据我所知)保持快速旋转的事件循环,从而使GUI保持响应。 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. 如果你想利用输入的GUI程序,你需要输入某种方式加入 GUI程序,如文本框和一个“提交”按钮。 Then, when the "submit" button is clicked, the input is read from the text box, and acted upon. 然后,当单击“提交”按钮时,将从文本框中读取输入并对其进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM