hugo-soyh
hugo-soyh

Reputation: 21

Black Screen while running exe file converted from py file with tkinter by Pyinstaller

I am creating a standalone executable GUI app using Python tkinter and pyinstaller. However, the exe file only shows the console and black screen (refer to the attached img). I have searched for answer for a week including checking other stack overflow posts, but no result. Hope you can help me, many thanks!

black screen

Here is the code:-

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()

Here is the environment:-

Python 3.9.4 
PyCharm 2021.1
macOS Big Sur 11.2.3

Here is how I used pyinstaller in terminal:-

cd /Users/abcd/PycharmProjects/WeatherApp
pyinstaller --onefile Weather.py

Here is what I have done to try to fix the problem:-

  1. Removed font=("Courier")
  2. Removed bg="#80c1ff"
  3. Created another very simple .py (just let user input name and output "Hello" + name) to be converted to exe. This simple .py is not tkinter. The converted exe runs normally.
  4. Adjusted pyinstaller --onefile Weather.py to pyinstaller --onefile --noconsole Weather.py

Upvotes: 2

Views: 2970

Answers (1)

Sasha Halpern
Sasha Halpern

Reputation: 153

Add --windowed when you run the command line ie

--onefile --windowed Weather.py

Upvotes: 3

Related Questions