【发布时间】: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