英文:
TKInter not working when I code in a function
问题
我正在编写一个使用航班API的简短程序。根据我的尝试,我希望将第30到59行(即“Client”之前,到“root.mainloop”之前的部分)放在函数flightInfo内。但是,当我按照当前的代码格式(如下所示)进行格式化时,我用于GUI的tkinter窗口正常工作。然而,当我将API代码(30-59行)移到函数内部时,我的TKinter窗口不会打开。任何帮助将不胜感激!我不明白为什么移动API代码(30-59行)会影响tkinter窗口。谢谢
from duffel_api import Duffel
from tkinter import *
root = Tk()
def flightInfo():
client = Duffel(access_token="token")
destination = input("\nWhere do you want to go?\n").strip()
origin = input("\nFrom where?\n").strip()
departure_date = input("\nOn what date? (YYYY-MM-DD)\n").strip()
print("\nSearching flights...")
slices = [
{
"origin": origin,
"destination": destination,
"departure_date": departure_date,
},
]
offer_request = (
client.offer_requests.create()
.passengers([{"type": "adult"}])
.slices(slices)
.return_offers()
.execute()
)
offers = offer_request.offers
for idx, offer in enumerate(offers):
print(
f"{idx + 1}. {offer.owner.name} flight departing at "
+ f"{offer.slices[0].segments[0].departing_at} "
+ f"{offer.total_amount} {offer.total_currency}"
)
destLabel = Label(root, text="Where do want to go?")
destLabel.grid(row=0,column=0)
dest = Entry(root)
dest.grid(row=0,column=1)
srcLabel = Label(root, text="From Where?")
srcLabel.grid(row=1,column=0)
src = Entry(root)
src.grid(row=1,column=1)
dateLabel = Label(root, text="On what date? (YYYY-MM-DD)")
dateLabel.grid(row=2,column=0)
date = Entry(root)
date.grid(row=2,column=1)
submit = Button(root, text="Submit", command=flightInfo)
submit.grid(row=3, column=0)
root.mainloop()
英文:
I am writing a short program that uses a flight api. For what I am trying to do, I want lines 30 to 59(where "Client" is, to right before "root.mainloop") to be inside the function flightInfo. When I have the code formatted as it is right now(see below) , the tkinter window I am using for my GUI works fine. However when I move the api code (30-59) inside the function, my TKinter window does not open. Any assistance would be appreciated! I don't see why moving the api code(30-59) messes with the tkinter window. Thank You
from duffel_api import Duffel
from tkinter import *
root = Tk()
def flightInfo():
client = Duffel(access_token="token")
destination = input("\nWhere do you want to go?\n").strip()
origin = input("\nFrom where?\n").strip()
departure_date = input("\nOn what date? (YYYY-MM-DD)\n").strip()
print("\nSearching flights...")
slices = [
{
"origin": origin,
"destination": destination,
"departure_date": departure_date,
},
]
offer_request = (
client.offer_requests.create()
.passengers([{"type": "adult"}])
.slices(slices)
.return_offers()
.execute()
)
offers = offer_request.offers
for idx, offer in enumerate(offers):
print(
f"{idx + 1}. {offer.owner.name} flight departing at "
+ f"{offer.slices[0].segments[0].departing_at} "
+ f"{offer.total_amount} {offer.total_currency}"
)
destLabel = Label(root, text = "Where do want to go?")
destLabel.grid(row=0,column=0)
dest = Entry(root)
dest.grid(row=0,column=1)
srcLabel = Label(root, text = "From Where?")
srcLabel.grid(row=1,column=0)
src = Entry(root)
src.grid(row=1,column=1)
dateLabel = Label(root, text = "On what date? (YYYY-MM-DD)")
dateLabel.grid(row=2,column=0)
date = Entry(root)
date.grid(row=2,column=1)
submit = Button(root,text = "Submit",command= flightInfo)
submit.grid(row=3,column=0)
root.mainloop
答案1
得分: 0
Notice below that this minimal code will open a window.
如果没有窗口打开,代码可能陷入无限循环。
I suggest starting adding widgets from the original code step by step to find the issue.
我建议从原始代码逐步添加小部件以找到问题。
from tkinter import *
root = Tk()
root.mainloop()
It might even be better to start from this code.
This way, at least the program starts with one widget working.
甚至从这段代码开始可能更好。
这样,至少程序会启动一个工作的小部件。
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="I Exist!")
label.pack()
root.mainloop()
英文:
Notice below that this minimal code will open a window.
If no window is opening, the code may be in an infinite loop.
I suggest starting adding widgets from the original code step by step to find the issue.
from tkinter import *
root = Tk()
root.mainloop()
It might even be better to start from this code.
This way, at least the program starts with one widget working.
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="I Exist!")
label.pack()
root.mainloop()
答案2
得分: 0
我很高兴我的评论解决了你的问题(我在这里将其作为正式答案,供其他人查看,如果你想投票支持它)。
解决方法是root.mainloop需要括号,所以它应该如下所示:
root.mainloop()
英文:
I'm glad my comment solved your problem (I've written it here as a formal answer for others to see if you want to upvote it).
The solution is that root.mainloop requires brackets, so it would look like:
> root.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论