Skip to content

Commit

Permalink
sun valley messageboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
not-nef committed Aug 30, 2023
1 parent 5efec8f commit 4610a4f
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 2 deletions.
190 changes: 190 additions & 0 deletions dialogs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# Property of rdbende licensed under the MIT License
# https://github.com/rdbende/Sun-Valley-messageboxes/blob/main/dialogs.py

import sys
import tkinter as tk
from tkinter import ttk
from functools import partial


def popup(parent, title, details, icon, *, buttons):
dialog = tk.Toplevel()

result = None

big_frame = ttk.Frame(dialog)
big_frame.pack(fill="both", expand=True)
big_frame.columnconfigure(0, weight=1)
big_frame.rowconfigure(0, weight=1)

info_frame = ttk.Frame(big_frame, padding=(10, 12), style="Dialog_info.TFrame")
info_frame.grid(row=0, column=0, sticky="nsew")
info_frame.columnconfigure(1, weight=1)
info_frame.rowconfigure(1, weight=1)

try:
color = big_frame.tk.call("set", "themeColors::dialogInfoBg")
except tk.TclError:
color = big_frame.tk.call("ttk::style", "lookup", "TFrame", "-background")

icon_label = ttk.Label(info_frame, image=icon, anchor="nw", background=color)
if icon is not None:
icon_label.grid(
row=0, column=0, sticky="nsew", padx=(12, 0), pady=10, rowspan=2
)

title_label = ttk.Label(
info_frame, text=title, anchor="nw", font=("", 14, "bold"), background=color
)
title_label.grid(row=0, column=1, sticky="nsew", padx=(12, 17), pady=(10, 8))

detail_label = ttk.Label(info_frame, text=details, anchor="nw", background=color)
detail_label.grid(row=1, column=1, sticky="nsew", padx=(12, 17), pady=(5, 10))

button_frame = ttk.Frame(
big_frame, padding=(22, 22, 12, 22), style="Dialog_buttons.TFrame"
)
button_frame.grid(row=2, column=0, sticky="nsew")

def on_button(value):
nonlocal result
result = value
dialog.destroy()

for index, button_value in enumerate(buttons):
style = None
state = None
default = False
sticky = "nes" if len(buttons) == 1 else "nsew"

if len(button_value) > 2:
if button_value[2] == "accent":
style = "Accent.TButton"
default = True
elif button_value[2] == "disabled":
state = "disabled"
elif button_value[2] == "default":
default = True

button = ttk.Button(
button_frame,
text=button_value[0],
width=18,
command=partial(on_button, button_value[1]),
style=style,
state=state,
)
if default:
button.bind("<Return>", button["command"])
button.focus()

button.grid(row=0, column=index, sticky=sticky, padx=(0, 10))

button_frame.columnconfigure(index, weight=1)

if sys.platform == "win32":
transparent_color = big_frame.tk.call("ttk::style", "lookup", "TFrame", "-background")
dialog.wm_attributes("-transparentcolor", transparent_color)

# dialog.overrideredirect(True)
dialog.update_idletasks()

dialog_width = dialog.winfo_width()
dialog_height = dialog.winfo_height()

if parent is None:
parent_width = dialog.winfo_screenwidth()
parent_height = dialog.winfo_screenheight()
parent_x = 0
parent_y = 0
else:
parent_width = parent.winfo_width()
parent_height = parent.winfo_height()
parent_x = parent.winfo_x()
parent_y = parent.winfo_y()

x_coord = int(parent_width / 2 + parent_x - dialog_width / 2)
y_coord = int(parent_height / 2 + parent_y - dialog_height / 2)

dialog.geometry("+{}+{}".format(x_coord, y_coord))
dialog.minsize(320, dialog_height)

dialog.transient(parent)
dialog.wm_attributes("-type", "dialog")
dialog.grab_set()

dialog.wait_window()
return result


def show_message(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Ok", None, "default")],
)


def ask_ok_cancel(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Ok", True, "accent"), ("Cancel", None)],
)


def ask_yes_no(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Yes", True, "accent"), ("No", False)],
)


def ask_yes_no_cancel(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Yes", True, "accent"), ("No", False), ("Cancel", None)],
)


def ask_retry_cancel(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Retry", True, "accent"), ("Cancel", None)],
)


def ask_allow_block(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Allow", True, "accent"), ("Block", False)],
)


if __name__ == "__main__":
window = tk.Tk()

window.tk.call("source", "sun-valley.tcl")
window.tk.call("set_theme", "dark")

window.geometry("600x600")

show_message("No WiFi connection", "Check your connection and try again.")

window.mainloop()
5 changes: 3 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from platform import system
from tkinter import Event, Menu, PhotoImage, Tk, messagebox, TclError
from tkinter.ttk import Button, Combobox, Entry, Frame, Label
from tkinter.messagebox import askyesno

from platformdirs import user_data_dir
from pyowm import OWM
Expand All @@ -16,6 +15,8 @@
from sv_ttk import set_theme
from webbrowser import open as openwebpage

from dialogs import ask_yes_no

VERSION = "0.1"

# Create constant
Expand Down Expand Up @@ -435,7 +436,7 @@ def check_for_updates(self) -> None:
self.latest_tag = self.api_response.json()["tag_name"].removeprefix("v")

if VERSION != self.latest_tag:
self.doupdate = askyesno("Update available!", "Do you want to update to the newest version?", master=self)
self.doupdate = ask_yes_no("Update available!", "Do you want to update to the newest version?")
if self.doupdate:
openwebpage(self.api_response.json()["html_url"])
self.destroy()
Expand Down

0 comments on commit 4610a4f

Please sign in to comment.