【问题标题】:Black Screen while running exe file converted from py file with tkinter by PyinstallerPyinstaller 使用 tkinter 运行从 py 文件转换的 exe 文件时出现黑屏
【发布时间】:2021-07-25 01:33:30
【问题描述】:

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

black screen

这里是代码:-

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

这是我为解决问题所做的工作:-

  1. 删除了 font=("Courier")
  2. 已移除 bg="#80c1ff"
  3. 创建了另一个非常简单的.py(只需让用户输入名称并输出“Hello”+名称)以转换为exe。这个简单的 .py 不是 tkinter。转换后的exe运行正常。
  4. 将 pyinstaller --onefile Weather.py 调整为 pyinstaller --onefile --noconsole Weather.py

【问题讨论】:

  • 听起来像一个 pyinstaller 问题,如果它没有转换的话——所以你应该关注它。无论如何,我建议你用except Exception as exc: 替换那个裸露的except:,并在它下面放一个缩进的print(exc)。这样您就会被告知发生的任何错误。

标签: python macos tkinter pyinstaller


【解决方案1】:

运行命令行时添加--windowed ie

--onefile --windowed Weather.py

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 2021-12-28
    • 2021-06-14
    相关资源
    最近更新 更多