【问题标题】:Why does Python tkinter crash when opening this file?为什么打开此文件时 Python tkinter 会崩溃?
【发布时间】:2020-08-27 10:13:11
【问题描述】:

有人知道为什么 Python Launcher 在运行此代码时会崩溃吗?有什么明显的我失踪了,因为它以前工作过。从终端在 Mac 上运行。

甚至不会抛出错误,所以我不太确定发生了什么。

我需要更新 tkinter 还是它只是导致问题的功能之一?

import tkinter as tk
import requests
import json
from PIL import ImageTk, Image

root = tk.Tk()
root.geometry("800x600")
# root.configure(bg = 'black')

# root.iconbitmap('./images/rain_jpeg.jpg')

Images = dict()

def display_photo(row=0, column=0):

    if weather_main + '_tk' not in [*Images]:
        image_1 = ImageTk.PhotoImage(Image.open("./images/" + weather_main + ".jpg"))
        photo = tk.Label(root, image = image_1)
        photo.image = image_1
        photo.pack()

    text_runner()

def city_clicker_runner():
    
    global weather_main
    global city

    city = e.get()
    request_address = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=0a0699452f695d2f9f82b65af024a134"
    api_request = requests.get(request_address)
    api = json.loads(api_request.content)

    mylabel2 = tk.Label(root, text = 'URL=' + request_address)
    # mylabel2.configure(foreground="white")
    mylabel2.pack()

    coordinates = api["coord"]
    weather_main = api["weather"][0]["main"]
    weather_description = api["weather"][0]["description"]
    temp = int(api["main"]["temp"])

    mainy = tk.Label(root, text = "the weather is " + weather_main)
    mainy.pack()

    display_photo()
        
def text_runner():

    running = tk.Label(root, text = 'we are running')
    running.pack()

    mylabel = tk.Label(root, text = "The weather in " + city + " is " + weather_main + " and the temperature is " + str(temp), font=("Helvetica", 20))
    mylabel.pack()

e = tk.Entry(root)
e.pack()

city_clicker = tk.Button(root, text = "GO", command=city_clicker_runner)
city_clicker.pack()

【问题讨论】:

标签: python tkinter terminal crash launcher


【解决方案1】:

没有root.mainloop(),应用程序将无法启动。就像应用程序总是关闭一样,您必须将其保持在循环中,只要您关闭它,它才能真正继续运行(跳出循环)

希望它清除了错误

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2016-07-09
    • 1970-01-01
    • 2018-08-20
    相关资源
    最近更新 更多