如何将功能的输入和输出从控制台转移到GUI应用程序



我不知道如何将函数的输入和输出从控制台转移到接口。我不知道我是否应该每次都执行destroy函数并创建一个新的框架。

from tkinter import *
states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
"Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming"]
capitals = ["Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta", "Honolulu", "Boise",
"Springfield", "Indianapolis", "Des Moines", "Topeka", "Frankfurt", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing", "St. Paul", "Jackson",
"Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus",
"Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City", "Montpelier", "Richmond",
"Olympia", "Charleston", "Madison", "Cheyenne"]
def geofunction():
i = 0
for element in states:
value = 1
while value == 1:
test = input(element + ": ") 
if test == capitals[i]:
print("You got it!")
value = 0
else:
print("Try again")
i += 1
print("Game Finished!")
root = Tk()
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack()
theLabel = Label(topFrame, text = "Welcome to the Geography Quizzer!nClick below to get started!")
theLabel.pack(fill=BOTH)
getstarted = Button(bottomFrame, text="Get started!", fg="black", bg="tan", command=geofunction)
getstarted.pack(fill=BOTH)
root.mainloop()

你不需要每次都破坏你的框架,你可以更改它。你可以在tour框架中放置一个标签,然后更改框架的文本。例如,如果您想更改theLabel的文本,您可以执行以下操作:

theLabel['text'] = 'new text'
root.update()

广告使用输入,我相信你更喜欢在GUI中将输入更改为入口小部件。Entry小部件是一个小部件,用户可以在其中键入输入,您可以使用Entry.get((或使用tkinter变量作为Entry.textvariable来访问它,例如:

from tkinter import *
states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
"Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming"]
capitals = ["Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta", "Honolulu", "Boise",
"Springfield", "Indianapolis", "Des Moines", "Topeka", "Frankfurt", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing", "St. Paul", "Jackson",
"Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus",
"Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City", "Montpelier", "Richmond",
"Olympia", "Charleston", "Madison", "Cheyenne"]
counter = 0
root = Tk()
def geofunction():
global counter
def get_answer():
global counter
if input_entry.get() == capitals[counter]:
theLabel['text'] = "You got it!"
counter += 1
input_text['text']=states[counter]+":"
else:
theLabel['text'] = "Try again"
if counter == len(states):
theLabel['text'] = "Game Finished!"
input_entry.destroy()

getstarted.pack_forget()
input_text = Label(text=states[counter]+":")
input_text.pack()
input_entry = Entry()
input_entry.pack()
Button(text='answer', command=get_answer).pack()

topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack()
theLabel = Label(topFrame, text = "Welcome to the Geography Quizzer!nClick below to get started!")
theLabel.pack(fill=BOTH)
getstarted = Button(bottomFrame, text="Get started!", fg="black", bg="tan", command=geofunction)
getstarted.pack(fill=BOTH)
root.mainloop()

相关内容

  • 没有找到相关文章