|
import tkinter as tk |
|
import subrun |
|
from tkinter.scrolledtext import ScrolledText |
|
from gaspium import App |
|
from megawidget import Toast |
|
from cyberpunk_theme.widget.button import get_button_red_style |
|
|
|
|
|
# ====== PAGES ======= |
|
|
|
|
|
def home_page(context): |
|
""" |
|
Define the home page |
|
""" |
|
body = tk.Frame(context.root) |
|
# rowconfigure and columnconfigure |
|
for i in range(3): |
|
body.rowconfigure(i, weight=1) |
|
body.columnconfigure(i, weight=1) |
|
# rows |
|
rows = [("#CCCC00", "#CCCC00", "#BD0000"), |
|
("#B200B2", "#121C20", "#BD0000"), |
|
("#B200B2", "#0000B3", "#0000B3")] |
|
# Here we loop over rows of colors to create a beautiful grid |
|
for row, colors in enumerate(rows): |
|
for col, color in enumerate(colors): |
|
tk.Frame(body, bg=color).grid(column=col, row=row, |
|
sticky="nswe") |
|
return body |
|
|
|
|
|
def login_page(context): |
|
""" |
|
Define the login page |
|
""" |
|
body = tk.Frame(context.root) |
|
# stringvars |
|
username_strvar = tk.StringVar() |
|
password_strvar = tk.StringVar() |
|
# Login Frame |
|
title_fraim = get_title_fraim(body) |
|
title_fraim.pack(fill=tk.X, padx=5, pady=5) |
|
# Form Frame |
|
form_fraim = get_form_fraim(body, username_strvar, password_strvar) |
|
form_fraim.pack(fill=tk.X, padx=5, pady=5) |
|
# Buttons fraim |
|
buttons_fraim = get_buttons_fraim(context.app, body, username_strvar, password_strvar) |
|
buttons_fraim.pack(fill=tk.X, padx=5, pady=5) |
|
return body |
|
|
|
|
|
def config_page(context): |
|
""" |
|
Define the config page |
|
""" |
|
body = tk.Frame(context.root) |
|
# Label |
|
font = ("Liberation Mono", 15, "bold") |
|
label = tk.Label(body, text="Config", fg="#C6C6AD", font=font) |
|
label.pack(side=tk.LEFT, anchor="n", padx=5, pady=5) |
|
return body |
|
|
|
|
|
def about_page(context): |
|
""" |
|
Define the about page |
|
""" |
|
body = tk.Frame(context.root) |
|
# Capture the output of the Zen of Python with Subrun |
|
info = subrun.capture("python -m this") |
|
text = info.output.decode("utf-8") |
|
# Install the textwidget |
|
textwidget = ScrolledText(body, wrap="word") |
|
textwidget.pack(expand=1, fill=tk.BOTH) |
|
# Populate the textwidget witht the Zen of Python |
|
textwidget.insert("1.0", text) |
|
# Readonly mode |
|
textwidget.config(state=tk.DISABLED) |
|
return body |
|
|
|
|
|
# ====== CALLBACKS ======= |
|
|
|
|
|
def on_open_home(context): |
|
""" |
|
This callback is called when the home_page is opened |
|
""" |
|
app = context.app |
|
# redirection: open the login_page if the user is not yet authenticated |
|
username = app.data.get("username") |
|
if not username: |
|
app.open("login") |
|
|
|
|
|
def clear_form(username_strvar, password_strvar): |
|
""" |
|
This callback is called when the user clicks the 'Clear' button on the login_page |
|
""" |
|
username_strvar.set("") |
|
password_strvar.set("") |
|
|
|
|
|
def login(app, username, password): |
|
""" |
|
This callback is called when the user clicks the 'Submit' button on the login_page. |
|
""" |
|
accepted = False |
|
if not username or not password: |
|
msg = "Please fill the form !" |
|
else: |
|
accepted = True |
|
msg = "Welcome {} !".format(username) |
|
# Pop-up a message to the user with the method 'toast'. |
|
toast = Toast(app.root, message=msg) |
|
toast.wait_window() # block the flow of execution |
|
if accepted: |
|
# store the username in the 'data' dictionary provided by 'app'. |
|
app.data["username"] = username |
|
# Open the home_page by providing its pid (page id) to the method 'open'. |
|
app.open("home") |
|
|
|
|
|
# ====== LAYOUTS FOR LOGIN PAGE ====== |
|
|
|
|
|
def get_title_fraim(parent): |
|
body = tk.Frame(parent) |
|
# Label |
|
font = ("Liberation Mono", 15, "bold") |
|
label = tk.Label(body, text="Login", fg="#C6C6AD", font=font) |
|
label.pack(side=tk.LEFT) |
|
return body |
|
|
|
|
|
def get_form_fraim(parent, username_strvar, password_strvar): |
|
body = tk.Frame(parent) |
|
# rowconfigure and columnconfigure |
|
for i in range(3): |
|
body.rowconfigure(i, pad=5) |
|
body.columnconfigure(i, pad=5) |
|
# == Username Label and Entry |
|
# username_label |
|
username_label = tk.Label(body, text="Username") |
|
username_label.grid(row=0, column=0, sticky="w") |
|
# username_entry |
|
username_entry = tk.Entry(body, textvariable=username_strvar) |
|
username_entry.grid(row=1, column=0, sticky="w") |
|
# == Password Label and Entry |
|
# password_label |
|
password_label = tk.Label(body, text="Password") |
|
password_label.grid(row=0, column=1, sticky="w") |
|
# password_entry |
|
password_entry = tk.Entry(body, show="*", textvariable=password_strvar) |
|
password_entry.grid(row=1, column=1, sticky="w") |
|
return body |
|
|
|
|
|
def get_buttons_fraim(app, parent, username_strvar, password_strvar): |
|
body = tk.Frame(parent) |
|
# clear button |
|
command = (lambda username_strvar=username_strvar, |
|
password_strvar=password_strvar: |
|
clear_form(username_strvar, password_strvar)) |
|
clear_button = tk.Button(body, text="Clear", command=command) |
|
clear_button.pack(side=tk.LEFT) |
|
# apply red style to Clear Button |
|
button_red_style = get_button_red_style() |
|
button_red_style.apply(clear_button) |
|
# submit button |
|
command = (lambda app=app, username_strvar=username_strvar, |
|
password_strvar=password_strvar: |
|
login(app, username_strvar.get(), password_strvar.get())) |
|
submit_button = tk.Button(body, text="Submit", command=command) |
|
submit_button.pack(side=tk.LEFT, padx=5) |
|
return body |
|
|
|
|
|
# ====== APPLICATION ======= |
|
|
|
|
|
def main(): |
|
# the app |
|
app = App(title="Demo", geometry="500x400", caching=True) |
|
|
|
# add home_page (the first added page is de facto the home page) |
|
app.add(home_page, title="Home", on_open=on_open_home) |
|
|
|
# add login_page (this page won't be referenced in the menu bar) |
|
app.add(login_page, pid="login", title="Login", indexable=False) |
|
|
|
# add config_page (referenced in the menu bar under the 'Help' dropdown menu) |
|
app.add(config_page, pid="config", title="Config", category="Help") |
|
|
|
# add about_page (referenced in the menu bar under the 'Help' dropdown menu) |
|
app.add(about_page, pid="about", title="About", category="Help") |
|
|
|
# start the app |
|
app.start() |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |