【问题标题】:How to use entry widgets in Tkinter in a loop如何在循环中使用 Tkinter 中的条目小部件
【发布时间】:2020-07-08 22:28:00
【问题描述】:

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

[>,

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

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

【问题讨论】:

    标签: python python-3.x tkinter widget


    【解决方案1】:

    主要问题是它不等待用户输入,所以它基本上是通过条目中所有空白值的循环,然后什么都没有填充。 (详情见代码中我的cmets)

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

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

    这将再次启动,但需要调整窗口大小,

    需要填写的打印参考变量,您可能需要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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 2023-03-25
      • 2018-09-15
      • 2022-06-28
      • 1970-01-01
      • 2014-10-16
      • 2020-11-19
      相关资源
      最近更新 更多