如何在循环中使用 Tkinter 中的条目小部件

How to use entry widgets in Tkinter in a loop

我正在制作一款包含大量用户输入的 Mad Libs 游戏。给定单词类型列表,我创建了一个函数来获取列表中的所有输入。没有错误消息,但 window 是空白的。我尝试打印 wordInputs 列表,结果显示:

[<bound method StringVar.get of <tkinter.StringVar object at 0x108763fd0>>,

等等。我认为这是因为它删除了所有的小部件,但我认为只有在我输入它们之后它才会这样做。我也不确定如何将输入存储到变量中;我为那个或什么创建一个按钮?这是代码:

from tkinter import *

class Game:
    #Gets user input for a specified type of word
    def getWord(self, words):
        wordInputs = []
        for i in range(len(words)):
            frame = Frame(self.windowSnowDay)
            frame.pack()
            Label(frame, text = "\nSnow Day\n________\n").grid(row = 1, column = 1, columnspan = 2)
            Label(frame, text = "Enter a(n) " + words[i] + ":").grid(row = 2, column = 1)
            word = StringVar()
            Entry(frame, textvariable = word).grid(row = i + 2, column = 2)
            wordInputs.append(word.get)
            frame.destroy()
        return wordInputs

    #Executes "Snow Day" story from Mad Libs menu
    def snowDay(self):
        self.windowMadLibs.destroy()
        self.windowSnowDay = Tk()
        self.windowSnowDay.title("Snow Day")
        self.windowSnowDay.geometry("200x200")
        frame = Frame(self.windowSnowDay)
        frame.pack()
        #Collects words and stores them in a list
        words = ["verb", "electronic device", "public place", "adjective", "verb ending with -ing", "color", "noun", "noun", "drink", \
            "clothing item", "adjective", "3-dimensional shape", "adjective", "plural noun", "adjective", "feeling (adjective)", "food"]
        wordInputs = self.getWord(words)
        print(wordInputs)
        self.windowSnowDay.mainloop()
        #Prints "Snow Day" story with all inputted words
        print("\nAll the children cheer in", emotion, "as they", verb, "that morning. They hear on the", device,"that the", place, end =' ')
        print("is closed because of a snowstorm. They think of all the", adj, "things they could do today, such as", verb1, "on a", end = ' ')
        print(color, noun + ". Maybe they can even make a snow" + noun1 + "! They go inside, where a warm cup of", drink, "awaits", end = ' ')
        print("them. Before going outside, they put on their scarves and", clothing, "so that they don't get", adj1 + ". They", end = ' ')
        print("make a snow" + noun1, "out of 3 large", shape + "s, but it quickly fell apart because the snow wasn't very", adj2, end = '. ')
        print("After that, one of the", noun2, "attacked another, and it turned into a", adj3, "snowball fight. They were so", feeling, end = ' that ')
        print("they went straight to bed. Oh well, I guess they can eat the leftovers of Mom's famous", food, "tomorrow!")

    #Main function for Mad Libs
    def madLibs(self):
        self.windowMadLibs = Tk()
        self.windowMadLibs.title("Mad Libs")
        self.windowMadLibs.geometry("200x200")
        frame = Frame(self.windowMadLibs)
        frame.pack()
        Label(frame, text = "\nMad Libs\n________\n").grid(row = 1, column = 1)
        Button(frame, text = "Snow Day", command = self.snowDay).grid(row = 2, column = 1)
        self.windowMadLibs.mainloop()

主要问题是它不等待用户输入,所以它基本上是在循环中输入所有空白值,然后什么都不填。 (更多细节见我在代码中的注释)

bound method StringVar.get 结果来自 wordInputs.append(word.get) 而不是 wordInputs.append(word.get())

此外,多次 Tk().mainloop() 调用往往会破坏工作。

这将使它再次运行,但是 window 需要解决调整大小的问题,

打印需要填写的参考变量,您可能需要 dictionarylist

from tkinter import *

class Game:
    #Gets user input for a specified type of word
    def getWord(self, words):           
        wordInputs = []
        for i in range(len(words)):
            frame = Frame(self.windowSnowDay)
            frame.pack()
            Label(frame, text = "\nSnow Day\n________\n").grid(row = 1, column = 1, columnspan = 2)
            Label(frame, text = "Enter a(n) " + words[i] + ":").grid(row = 2, column = 1)
            word = StringVar()
            entry = Entry(frame)
            entry.grid(row = i + 2, column = 2)
            button = Button(frame, text = "Done", command = lambda: word.set(entry.get()))
            button.grid(row = i + 3, column = 2)
            button.wait_variable(word) # waits for button to be activated and sets the variable.
            wordInputs.append(word.get())
            frame.destroy()
        return wordInputs

    #Executes "Snow Day" story from Mad Libs menu
    def snowDay(self):
        self.windowMadLibs.withdraw() # withdraw the main window.
        self.windowSnowDay = Toplevel(self.windowMadLibs) # create a Toplevel instead of a new main.
        self.windowSnowDay.title("Snow Day")
        self.windowSnowDay.geometry("200x200")
        frame = Frame(self.windowSnowDay)
        frame.pack()
        #Collects words and stores them in a list
        words = ["verb", "electronic device", "public place", "adjective", "verb ending with -ing", "color", "noun", "noun", "drink", \
            "clothing item", "adjective", "3-dimensional shape", "adjective", "plural noun", "adjective", "feeling (adjective)", "food"]
        wordInputs = self.getWord(words)
        print(wordInputs)
        #Prints "Snow Day" story with all inputted words
        print("\nAll the children cheer in", emotion, "as they", verb, "that morning. They hear on the", device,"that the", place, end =' ')
        print("is closed because of a snowstorm. They think of all the", adj, "things they could do today, such as", verb1, "on a", end = ' ')
        print(color, noun + ". Maybe they can even make a snow" + noun1 + "! They go inside, where a warm cup of", drink, "awaits", end = ' ')
        print("them. Before going outside, they put on their scarves and", clothing, "so that they don't get", adj1 + ". They", end = ' ')
        print("make a snow" + noun1, "out of 3 large", shape + "s, but it quickly fell apart because the snow wasn't very", adj2, end = '. ')
        print("After that, one of the", noun2, "attacked another, and it turned into a", adj3, "snowball fight. They were so", feeling, end = ' that ')
        print("they went straight to bed. Oh well, I guess they can eat the leftovers of Mom's famous", food, "tomorrow!")

    #Main function for Mad Libs
    def madLibs(self):
        self.windowMadLibs = Tk()
        self.windowMadLibs.title("Mad Libs")
        self.windowMadLibs.geometry("200x200")
        frame = Frame(self.windowMadLibs)
        frame.pack()
        Label(frame, text = "\nMad Libs\n________\n").grid(row = 1, column = 1)
        Button(frame, text = "Snow Day", command = self.snowDay).grid(row = 2, column = 1)
        self.windowMadLibs.mainloop()

g = Game()
g.madLibs()