Skip to content

Commit

Permalink
Remove progressbar
Browse files Browse the repository at this point in the history
Couldn't load before the results came in so effectively useless.
  • Loading branch information
Moosems committed Aug 15, 2023
1 parent a51ea38 commit ef949f0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 85 deletions.
71 changes: 2 additions & 69 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,6 @@
from requests import Response
from requests import get as requests_get


class ProgressBar(Toplevel):
def __init__(self, parent: App, *args, **kwargs):
# Set up window
super().__init__(parent, *args, **kwargs)
self.title("Loading...")
self.resizable(False, False)

# Set up widgets
self.main_frame = Frame(self)
self.main_frame.grid()

main_label = Label(self.main_frame, text="Loading...", font="Helvetica 15 bold")
main_label.grid(padx=10, pady=10)

self.progressbar = Progressbar(
self.main_frame,
orient="horizontal",
length=200,
mode="indeterminate",
maximum=4,
)
self.progressbar.grid(padx=10, pady=10)

self.resize_app()

def resize_app(self) -> App:
"""Use tkinter to detect the minimum size of the app, get the center of the screen, and place the app there."""

# TODO: Make a global function for this to remove boilerplate code
# Update widgets so minimum size is accurate
self.update_idletasks()

# Get minimum size
minimum_width: int = self.winfo_reqwidth()
minimum_height: int = self.winfo_reqheight()

# Get center of screen based on minimum size
x_coords = int(self.winfo_screenwidth() / 2 - minimum_width / 2)
y_coords = int(self.wm_maxsize()[1] / 2 - minimum_height / 2)

# Place app and make the minimum size the actual minimum size (non-infringable)
self.geometry(f"{minimum_width}x{minimum_height}+{x_coords}+{y_coords}")
self.wm_minsize(minimum_width, minimum_height)
return self

def set_progress(self, progress: int):
self.progressbar.step(progress)


class App(Tk):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -147,17 +97,16 @@ def __init__(self):
self.resize_app()
self.deiconify()

def about(self) -> App:
def about(self) -> None:
"""Display a messagebox with information about the app."""

messagebox.showinfo(
"About Weather",
"Weather is a simple weather app that uses the OpenWeatherMap API to get the weather for a given city.",
parent=self,
)
return self

def resize_app(self) -> App:
def resize_app(self) -> None:
"""Use tkinter to detect the minimum size of the app, get the center of the screen, and place the app there."""

# Update widgets so minimum size is accurate
Expand All @@ -174,7 +123,6 @@ def resize_app(self) -> App:
# Place app and make the minimum size the actual minimum size (non-infringable)
self.geometry(f"{minimum_width}x{minimum_height}+{x_coords}+{y_coords}")
self.wm_minsize(minimum_width, minimum_height)
return self

def exit_app(self) -> None:
"""Exit the app."""
Expand All @@ -192,7 +140,6 @@ def OWMCITY(self, _: Event | None = None) -> None:
self.start_button.configure(state="disabled")
self.searchbar.configure(state="disabled")
self.update_labels()
pb = ProgressBar(self)

# Get API key
api_key: str = "c439e1209216cc7e7c73a3a8d1d12bfd"
Expand All @@ -209,13 +156,10 @@ def OWMCITY(self, _: Event | None = None) -> None:
self.update_labels()
self.start_button.configure(state="normal")
self.searchbar.configure(state="normal")
pb.destroy()
self.searching = False
self.resize_app() # In case the name gets too long or it renders differently on other systems
return

pb.progressbar.step()

# Check if city exists
try:
observation = mgr.weather_at_place(city)
Expand All @@ -224,13 +168,10 @@ def OWMCITY(self, _: Event | None = None) -> None:
self.update_labels()
self.start_button.configure(state="normal")
self.searchbar.configure(state="normal")
pb.destroy()
self.searching = False
self.resize_app() # In case the name gets too long or it renders differently on other systems
return

pb.progressbar.step()

# Get weather data
weather = observation.weather

Expand All @@ -245,13 +186,10 @@ def OWMCITY(self, _: Event | None = None) -> None:
self.update_labels()
self.start_button.configure(state="normal")
self.searchbar.configure(state="normal")
pb.destroy()
self.searching = False
self.resize_app() # In case the name gets too long or it renders differently on other systems
return

pb.progressbar.step()

# Get response data, simplify and create variables for usage
# TODO: Give all data in dual columns
# | Weather: Clear | Temp: 20°C |
Expand All @@ -278,14 +216,9 @@ def OWMCITY(self, _: Event | None = None) -> None:
# Set the city name
self.cityname.configure(text=f"City: {city}")

# Update the Progress Bar one last time
print("Updating Progress Bar one last time")
pb.progressbar.step()

# Stop the Progress Bar, enable buttons and searchbar, and set searching to False
self.start_button.configure(state="normal")
self.searchbar.configure(state="normal")
pb.destroy()
self.searching = False
self.resize_app() # In case the name gets too long or it renders differently on other systems

Expand Down
83 changes: 67 additions & 16 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,80 @@
import tkinter as tk
from __future__ import annotations

from platform import system
from tkinter import Event, Menu, Tk, Toplevel, messagebox
from tkinter.ttk import Button, Entry, Frame, Label, Progressbar, Style

class SampleApp(tk.Tk):
from pyowm import OWM
from pyowm.commons.exceptions import APIRequestError
from pyowm.commons.exceptions import NotFoundError as OWMNotFoundError
from requests import Response
from requests import get as requests_get


class ProgressBar(Toplevel):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# Set up window
Toplevel.__init__(self, *args, **kwargs)
self.withdraw()
self.title("Loading...")
self.resizable(False, False)

# Set up widgets
self.main_frame = Frame(self)
self.main_frame.grid()

main_label = Label(self.main_frame, text="Loading...", font="Helvetica 15 bold")
main_label.grid(padx=10, pady=10)

self.progressbar = Progressbar(
self.main_frame,
orient="horizontal",
length=200,
mode="indeterminate",
maximum=4,
)
self.progressbar.grid(padx=10, pady=10)

self.resize_app()
self.update_idletasks()
self.after_idle(lambda: self.deiconify())

def resize_app(self) -> None:
"""Use tkinter to detect the minimum size of the app, get the center of the screen, and place the app there."""

# TODO: Make a global function for this to remove boilerplate code
# Update widgets so minimum size is accurate
self.update_idletasks()

# Get minimum size
minimum_width: int = self.winfo_reqwidth()
minimum_height: int = self.winfo_reqheight()

# Get center of screen based on minimum size
x_coords = int(self.winfo_screenwidth() / 2 - minimum_width / 2)
y_coords = int(self.wm_maxsize()[1] / 2 - minimum_height / 2)

self.frame = tk.Frame(self)
self.frame.pack(side="top", fill="both", expand=True)
# Place app and make the minimum size the actual minimum size (non-infringable)
self.geometry(f"{minimum_width}x{minimum_height}+{x_coords}+{y_coords}")
self.wm_minsize(minimum_width, minimum_height)

self.label = tk.Label(self, text="Hello, world")
self.label.pack(in_=self.frame)
def set_progress(self, progress: int):
self.progressbar.step(progress)

button1 = tk.Button(self, text="Click to hide label", command=self.hide_label)
button1.pack()

button2 = tk.Button(self, text="Click to show label", command=self.show_label)
button2.pack()
class App(Tk):
def __init__(self):
super().__init__()
self.bind("<Return>", self.OWMCITY)
self.deiconify()

def show_label(self, *_):
self.label.lift(self.frame)
def OWMCITY(self, _: Event | None = None) -> None:
"""Get the weather for a given city using the OpenWeatherMap API and display it in a label."""

def hide_label(self, *_):
self.label.lower(self.frame)
pb = ProgressBar()
# pb.destroy()


if __name__ == "__main__":
app = SampleApp()
app = App()
app.mainloop()

0 comments on commit ef949f0

Please sign in to comment.