-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_window.py
More file actions
128 lines (116 loc) · 5.39 KB
/
Copy pathmain_window.py
File metadata and controls
128 lines (116 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# encoding = utf-8
import re
import platform
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from i18n import I18n
import widgets
import settings_window
import about_window
import style
def empty_function():
raise NotImplementedError("The function is not implemented yet.")
class MainWindow(Tk):
def __init__(self):
super().__init__()
self.title("You-Get GUI")
self.geometry("500x280")
self.resizable(False, False)
self.iconbitmap("assets/icon.ico")
self.settings_window = None
self.about_window = None
style.init_styles(self)
self.create_widgets()
def create_widgets(self):
self.main_menu = tk.Menu(
self,
borderwidth=0,
background="#202020",
foreground="#ffffff",
activebackground="#6e6e6e",
activeforeground="#ffffff",
)
self.config(menu=self.main_menu)
self.main_menu.add_command(
label=I18n().get("main_window.main_menu.settings"),
command=self.show_settings_window,
)
self.main_menu.add_command(
label=I18n().get("main_window.main_menu.about"),
command=self.show_about_window,
)
self.logo_image_ph = PhotoImage(file="assets/logo.png")
self.logo_label = Label(self, image=self.logo_image_ph, style="Logo.TLabel")
self.main_f=Frame(self,borderwidth=0,style="blackf.TFrame")
self.url_f=Frame(self.main_f,borderwidth=0,style="blackf.TFrame")
self.url_e=widgets.PlaceholderEntry(self.url_f,
fieldbackground="#202020",
placeholder_color="#444444",
normal_color="#BDBDBD",
placeholder=I18n().get("main_window.url_placeholder"))
self.url_e.config(font=("Arial Narrow", 9))
self.url_b=widgets.Button(self.url_f, text=I18n().get("main_window.download_button"),
bg_normal="#202020", fg_normal="#ffffff",
bg_hover="#6e6e6e", fg_hover="#ffffff",
bg_press="#9e9e9e", fg_press="#ffffff",
font=("Arial Narrow", 10, ),
borderwidth=1,
height=15,
highlightthickness=1,
highlightbackground="#6e6e6e",)
self.status_f=Frame(self,borderwidth=0, style="blackf.TFrame")
self.status_processbar=Frame(self.status_f,borderwidth=0,height=5,
width=0,style="lightf.TFrame")
self.status_label=Label(self.status_f, font=("Segoe UI", 10), style="prompt.TLabel",
text=I18n().get("main_window.download_status.pending"))
def bind_download(self, callback):
self.url_b.config(command=callback)
# Bind Enter/Return to the entry widget so the callback only fires
# when the user presses Enter while focused in the URL entry.
try:
self.url_e.bind("<Return>", lambda e: callback())
self.url_e.bind("<KP_Enter>", lambda e: callback())
except Exception:
# fallback: bind to root Return event but ensure focus is on url_e
self.bind("<Return>", lambda e: callback() if self.focus_get() == self.url_e else None)
return callback
def show_settings_window(self):
if self.settings_window is None or not self.settings_window.winfo_exists():
self.settings_window = settings_window.SettingsWindow(self)
self.settings_window.deiconify()
self.settings_window.lift()
self.settings_window.focus_force()
def show_about_window(self):
if self.about_window is None or not self.about_window.winfo_exists():
self.about_window = about_window.AboutWindow(self)
self.about_window.deiconify()
self.about_window.lift()
self.about_window.focus_force()
def pack(self):
self.logo_label.pack(pady=10,side=TOP)
self.url_e.pack(side=LEFT, fill=X, expand=True,ipady=4)
self.url_b.pack(side=RIGHT, fill=Y, pady=1 if platform.system()=='Windows' else None)
self.url_f.pack(side=TOP, fill=X)
self.status_processbar.pack(anchor=SW, pady=(0, 2))
self.status_label.pack(anchor=SW)
self.status_f.pack(side=BOTTOM, padx=10, pady=(0, 5), fill=X)
self.main_f.pack(side=BOTTOM, padx=10, fill=X)
def set_process(self, value:float, piece:str="", speed:str="", received_fraction:str=""):
self.status_label.config(text=I18n().get("main_window.download_status.downloading")
.format(received_fraction=received_fraction,
progress="%.2f"%(value*100), piece=piece, speed=speed))
self.status_processbar.config(width=self.status_f.winfo_width() * value)
def demo_download(self):
print(f"Downloading from: {self.url_e.get()}")
self.set_process(0)
url_pattern = r'https?://[^\s]+'
match = re.search(url_pattern, self.url_e.get())
if not match:
print("Invalid URL")
return
if __name__ == "__main__":
app = MainWindow()
app.bind_download(app.demo_download)
app.pack()
app.mainloop()