Python 运行由Pyinstaller使用tkinter从py文件转换而来的exe文件时出现黑屏

Python 运行由Pyinstaller使用tkinter从py文件转换而来的exe文件时出现黑屏,python,macos,tkinter,pyinstaller,Python,Macos,Tkinter,Pyinstaller,我正在使用Python tkinter和pyinstaller创建一个独立的可执行GUI应用程序。但是,exe文件仅显示控制台和黑屏(请参阅随附的img)。我已经搜索了一周的答案,包括检查其他堆栈溢出帖子,但没有结果。希望你能帮助我,非常感谢 代码如下:- import tkinter as tk from tkinter import font import requests HEIGHT = 700 WIDTH = 800 def test_function(entry):

我正在使用Python tkinter和pyinstaller创建一个独立的可执行GUI应用程序。但是,exe文件仅显示控制台和黑屏(请参阅随附的img)。我已经搜索了一周的答案,包括检查其他堆栈溢出帖子,但没有结果。希望你能帮助我,非常感谢

代码如下:-

import tkinter as tk
from tkinter import font
import requests


HEIGHT = 700
WIDTH = 800

def test_function(entry):
    print("This is the entry: ", entry)

def format_response(weather):
    try:
        name = weather["name"]
        desc = weather["weather"][0]["description"]
        temp = weather["main"]["temp"]

        final_str = "City: %s \nConditions: %s \nTemperature: %s" %(name, desc, temp)
    except:
        final_str = "There was a problem retrieving that information"

    return final_str

def get_weather(city):
    weather_key = "5164b0c1b7f4019423208f1e0af93cbf"
    url = "https://api.openweathermap.org/data/2.5/weather"
    params = {"APPID": weather_key, "q":city, "units":"metric"}
    response = requests.get(url, params=params)
    weather = response.json()

    label["text"] = format_response(weather)

root = tk.Tk()

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

frame = tk.Frame(root, bg="#80c1ff", bd=5)
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor="n")

entry = tk.Entry(frame, font=("Courier", 18))
entry.place(relwidth=0.65, relheight=1)

button = tk.Button(frame, text="Get Weather", font=("Courier", 12), command=lambda: get_weather(entry.get()))
button.place(relx=0.7, relwidth=0.3, relheight=1)

lower_frame = tk.Frame(root, bg="#80c1ff", bd=10)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor="n")

label = tk.Label(lower_frame, font=("Courier", 18), anchor="nw", justify="left", bd=4)
label.place(relwidth=1, relheight=1)

root.mainloop()
以下是环境:-

Python 3.9.4 
PyCharm 2021.1
macOS Big Sur 11.2.3
以下是我如何在终端中使用pyinstaller:-

cd /Users/abcd/PycharmProjects/WeatherApp
pyinstaller --onefile Weather.py
以下是我为解决问题所做的努力:-

  • 删除字体=(“快递”)
  • 已删除bg=“#80c1ff”
  • 创建了另一个非常简单的.py(只需让用户输入名称并输出“Hello”+name)即可转换为exe。这个简单的.py不是tkinter。转换后的exe运行正常
  • 将pyinstaller--onefile Weather.py调整为pyinstaller--onefile--noconsole Weather.py

  • 如果pyinstaller无法转换,听起来像是一个问题,所以你应该关注这个问题。无论如何,我建议您将裸露的
    Exception:
    替换为
    Exception作为exc:
    ,并在其下方放置缩进的
    print(exc)
    。这样,发生的任何错误都会通知您。