From bc0d46d633e7279c47d066fa951e588e8f61dc62 Mon Sep 17 00:00:00 2001 From: punix Date: Fri, 8 Nov 2024 20:49:59 +0100 Subject: [PATCH] - methods from class MainWindow move to class FrameWidgets for active color_label when theme change - optimize columnconfigure, rowconfigure in class MainWindow and FrameWidgets - add new Frame for Widgets on Bottom - optimize from tkinter * to from tkinter import filedialog, ttk, TclError --- Changelog | 10 ++++ wg_func.py | 2 +- wg_main.py | 161 ++++++++++++++++++++++++++--------------------------- 3 files changed, 89 insertions(+), 84 deletions(-) diff --git a/Changelog b/Changelog index 890e2aa..4b9b8b6 100644 --- a/Changelog +++ b/Changelog @@ -8,11 +8,21 @@ My standard System: Linux Mint 22 Cinnamon - for loops with lists replaced by List Comprehensions - Update search after start of Wire-Py + + ### Added +08-11-2024 + +- methods from class MainWindow move to class FrameWidgets for active color_label when theme change +- optimize columnconfigure, rowconfigure in class MainWindow and FrameWidgets +- add new Frame for Widgets on Bottom +- optimize from tkinter * to from tkinter import filedialog, ttk, TclError + ### Added 07-11-2024 - remove classes and add methods to class FrameWidgets (removed self errors) + ### Added 27-10-2024 diff --git a/wg_func.py b/wg_func.py index 453ebb7..0486944 100755 --- a/wg_func.py +++ b/wg_func.py @@ -14,7 +14,7 @@ import requests ''' 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year ''' -version = 'v. 1.11.0724' +version = 'v. 1.11.0824' path_to_file2 = Path('/etc/wire_py/settings.conf') _u = Path.read_text(Path('/tmp/_u')) diff --git a/wg_main.py b/wg_main.py index 48432d7..e2cc83d 100755 --- a/wg_main.py +++ b/wg_main.py @@ -5,8 +5,7 @@ import subprocess import tkinter as tk from pathlib import Path from subprocess import check_call -from tkinter import * -from tkinter import filedialog, ttk +from tkinter import filedialog, ttk, TclError from wg_func import (Tunnel, msg_window, WirePyUpdate, res, _u, version, path_to_file2, tips) @@ -20,12 +19,15 @@ class MainWindow(tk.Tk): self.my_tool_tip = None self.x_width = 600 - self.y_height = 400 + self.y_height = 383 self.monitor_center_x = self.winfo_screenwidth() / 2 - (self.x_width / 2) self.monitor_center_y = self.winfo_screenheight() / 2 - (self.y_height / 2) - self.resizable(width=False, height=False) + self.resizable(width=True, height=True) self.title('Wire-Py') self.geometry('%dx%d+%d+%d' % (self.x_width, self.y_height, self.monitor_center_x, self.monitor_center_y)) + self.columnconfigure(0, weight=1) + self.rowconfigure(0, weight=1) + self.style = ttk.Style(self) self.tk.call('source', str(tcl_path) + '/water.tcl') ''' self.tk.call('source', 'TK-Themes/water.tcl') ''' @@ -42,6 +44,28 @@ class MainWindow(tk.Tk): ''' Set it as the window icon ''' self.iconphoto(True, self.wg_icon) + FrameWidgets(self).grid() + + +class FrameWidgets(ttk.Frame): + def __init__(self, container, **kwargs): + super().__init__(container, **kwargs) + + self.lb_tunnel = None + self.btn_stst = None + self.endpoint = None + self.dns = None + self.address = None + self.auto_con = None + self.wg_vpn_start = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-start.png') + self.wg_vpn_stop = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-stop.png') + self.imp_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_import.png') + self.tr_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_trash.png') + self.exp_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_export.png') + self.warning_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/64/error.png') + self.wg_vpn_start = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-start.png') + self.wg_vpn_stop = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-stop.png') + ''' Set on or off in file ''' def update(): @@ -59,7 +83,7 @@ class MainWindow(tk.Tk): with open(path_to_file2, 'w') as set_file2: set_file2.writelines(lines2) - ''' Set on or off in file ''' + ''' Set True or False in file ''' def tooltip(): if set_tip.get(): @@ -87,6 +111,7 @@ class MainWindow(tk.Tk): lines3[3] = 'light\n' with open(path_to_file2, 'w') as theme_set2: theme_set2.writelines(lines3) + self.color_label() def theme_change_dark(): if not self.tk.call("ttk::style", "theme", "use") == "water-dark": @@ -97,6 +122,7 @@ class MainWindow(tk.Tk): lines4[3] = 'dark\n' with open(path_to_file2, 'w') as theme_set2: theme_set2.writelines(lines4) + self.color_label() def info(): def link_btn(): @@ -120,14 +146,12 @@ class MainWindow(tk.Tk): ''' Frame for Menu ''' self.menu_frame = ttk.Frame(self) self.menu_frame.configure(relief='flat') - self.menu_frame.grid(column=0, row=0, sticky='w') - self.columnconfigure(0, weight=1) - self.rowconfigure(0, weight=1) + self.menu_frame.grid(column=0, row=0, columnspan=4, sticky='w') ''' App Menu ''' self.version_lb = ttk.Label(self.menu_frame, text=version) self.version_lb.config(font=('Ubuntu', 11), foreground='#00c4ff') - self.version_lb.grid(column=0, row=0, padx=10) + self.version_lb.grid(column=0, row=0, rowspan=4, padx=10) def version_enter(event): """ The mouse moves into the Version widget """ @@ -167,13 +191,11 @@ class MainWindow(tk.Tk): ''' About BTN Menu / Label ''' self.about_btn = ttk.Button(self.menu_frame, text='About', style='Toolbutton', command=info) self.about_btn.grid(column=2, row=0) - self.readme = tk.Menu(self) ''' Update and Tooltip Label ''' self.updates_lb = ttk.Label(self.menu_frame) self.updates_lb.grid(column=3, row=0, padx=10) - '''View Checkbox for enable or disable Tooltip ''' if tips: set_tip.set(value=False) @@ -237,29 +259,6 @@ class MainWindow(tk.Tk): self.update_btn.configure(menu=self.download, style='Toolbutton') self.download.add_command(label='Download', command=WirePyUpdate.download) - - FrameWidgets(self).grid() - - -class FrameWidgets(ttk.Frame): - def __init__(self, container, **kwargs): - super().__init__(container, **kwargs) - - self.lb_tunnel = None - self.btn_stst = None - self.endpoint = None - self.dns = None - self.address = None - self.auto_con = None - self.wg_vpn_start = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-start.png') - self.wg_vpn_stop = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-stop.png') - self.imp_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_import.png') - self.tr_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_trash.png') - self.exp_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_export.png') - self.warning_pic = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/64/error.png') - self.wg_vpn_start = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-start.png') - self.wg_vpn_stop = tk.PhotoImage(file=r'/usr/share/icons/wp-icons/48/wg_vpn-stop.png') - ''' Show active Tunnel ''' self.a = Tunnel.active() @@ -267,30 +266,27 @@ class FrameWidgets(ttk.Frame): self.lb_frame_btn_lbox = ttk.Frame(self) self.lb_frame_btn_lbox.configure(relief='flat') self.lb_frame_btn_lbox.grid(column=0, rowspan=3, row=1) - self.columnconfigure(0, weight=1) - self.rowconfigure(1, weight=1) ''' Label Frame 2 ''' self.lb_frame = ttk.Frame(self) self.lb_frame.configure(relief='solid') self.lb_frame.grid(column=2, row=2, sticky='snew', padx=20, pady=5) - self.columnconfigure(2, weight=1) - self.rowconfigure(2, weight=1) ''' Label Frame 3 ''' self.lb_frame2 = ttk.Frame(self) self.lb_frame2.configure(relief='solid') self.lb_frame2.grid(column=2, row=3, sticky='snew', padx=20, pady=5) - self.columnconfigure(2, weight=1) - self.rowconfigure(3, weight=1) + + ''' Bottom Frame 4 ''' + self.lb_frame3 = ttk.Frame(self) + self.lb_frame3.configure(relief='flat') + self.lb_frame3.grid(column=0, row=5, columnspan=4, sticky='snew', padx=2, pady=2) ''' Show active Label ''' self.select_tunnel = None self.lb = ttk.Label(self, text='Active: ') self.lb.config(font=('Ubuntu', 11, 'bold')) self.lb.grid(column=2, row=1, padx=15, pady=4, sticky='w') - self.columnconfigure(2, weight=1) - self.rowconfigure(0, weight=1) ''' Label to Show active Tunnel ''' self.StrVar = tk.StringVar(value=self.a) @@ -324,7 +320,6 @@ class FrameWidgets(ttk.Frame): self.scrollbar = ttk.Scrollbar(self.lb_frame_btn_lbox, orient='vertical', command=self.l_box.yview) self.scrollbar.grid(column=1, rowspan=4, row=0, sticky='nse') self.l_box.configure(yscrollcommand=self.scrollbar.set) - self.rowconfigure(0, weight=1) ''' Tunnel List ''' self.tl = Tunnel.list() @@ -546,8 +541,8 @@ class FrameWidgets(ttk.Frame): self.btn_exp.bind('', exp_leave) ''' Label Entry ''' - self.lb_rename = ttk.Entry(self, width=20) - self.lb_rename.grid(column=2, row=4, padx=30, pady=15, sticky='nw') + self.lb_rename = ttk.Entry(self.lb_frame3, width=20) + self.lb_rename.grid(column=2, row=0, padx=8, pady=10, sticky='ne') self.lb_rename.insert(0, 'Max. 12 characters!') self.lb_rename.config(state='disable') @@ -647,21 +642,21 @@ class FrameWidgets(ttk.Frame): msg_window(iw, ii, wt, msg_t) ''' Button Rename ''' - self.btn_rename = ttk.Button(self, text='Rename', state='disable', command=tl_rename, padding=4, + self.btn_rename = ttk.Button(self.lb_frame3, text='Rename', state='disable', command=tl_rename, padding=4, style='RnButton.TButton') - self.btn_rename.grid(column=2, row=4, padx=20, pady=15, sticky='ne') + self.btn_rename.grid(column=3, row=0, padx=5, pady=10, sticky='ne') ''' Check Buttons ''' self.selected_option = tk.IntVar() self.autoconnect_var = tk.StringVar() self.autoconnect_var.set(self.auto_con) ''' Frame for Labels, Entry and Button''' - self.autoconnect = ttk.Label(self, textvariable=self.autoconnect_var) + self.autoconnect = ttk.Label(self.lb_frame3, textvariable=self.autoconnect_var, width=15) self.autoconnect.config(font=('Ubuntu', 11)) - self.autoconnect.grid(column=0, row=4, sticky='ne', padx=10, pady=15) - self.wg_autostart = ttk.Checkbutton(self, text='Autoconnect on:', variable=self.selected_option, + self.autoconnect.grid(column=1, row=0, sticky='e', pady=19) + self.wg_autostart = ttk.Checkbutton(self.lb_frame3, text='Autoconnect on:', variable=self.selected_option, command=self.box_set) - self.wg_autostart.grid(column=0, row=4, pady=15, padx=15, sticky='nw') + self.wg_autostart.grid(column=0, row=0, pady=15, padx=15, sticky='nw') def chk_enter(event): """ The mouse moves into the entry widget """ @@ -707,19 +702,17 @@ class FrameWidgets(ttk.Frame): self.on_off() - """ - Import Methode for Wireguard config Files. - Before importing, it is checked whether PrivateKey and PublicKey are in the file. - If True then it is checked whether the PreSharedKey is already in the key file - to avoid an import error so that no double wgconf are imported. - Thus, tunnels can be renamed without the problems arise. If False then the key is written into the file. - Furthermore, it is checked whether the name is longer than 12 characters. - If True then the name is automatically shortened to 12 characters and then imported. - If in each case false comes out, a corresponding window comes to inform the user that something is wrong. - """ - def import_sl(self): - + """ + Import Methode for Wireguard config Files. + Before importing, it is checked whether PrivateKey and PublicKey are in the file. + If True then it is checked whether the PreSharedKey is already in the key file + to avoid an import error so that no double wgconf are imported. + Thus, tunnels can be renamed without the problems arise. If False then the key is written into the file. + Furthermore, it is checked whether the name is longer than 12 characters. + If True then the name is automatically shortened to 12 characters and then imported. + If in each case false comes out, a corresponding window comes to inform the user that something is wrong. + """ try: filepath = filedialog.askopenfilename(initialdir=str(_u), title='Select Wireguard config File', filetypes=[('WG config files', '*.conf')], ) @@ -875,14 +868,12 @@ class FrameWidgets(ttk.Frame): print('Tunnel exist!') - ''' - This Method will display the autostart label which - Tunnel is automatically started regardless of the active tunnel. - The selected tunnel is written into a file to read it after the start of the system. - ''' - def box_set(self): - + """ + This Method will display the autostart label which + Tunnel is automatically started regardless of the active tunnel. + The selected tunnel is written into a file to read it after the start of the system. + """ try: select_tunnel = self.l_box.curselection() select_tl = self.l_box.get(select_tunnel[0]) @@ -911,13 +902,12 @@ class FrameWidgets(ttk.Frame): self.on_off() - ''' - Here it is checked whether the path to the file is there if not it is created. - Set (on), the selected tunnel is displayed in the label. - At (off) the label is first emptied then filled with No Autoconnect - ''' - def on_off(self): + """ + Here it is checked whether the path to the file is there if not it is created. + Set (on), the selected tunnel is displayed in the label. + At (off) the label is first emptied then filled with No Autoconnect + """ with open(path_to_file2, 'r') as set_file4: lines4 = set_file4.readlines() @@ -934,16 +924,16 @@ class FrameWidgets(ttk.Frame): self.autoconnect_var = tk.StringVar() self.autoconnect_var.set(self.auto_con) - self.autoconnect = ttk.Label(self, textvariable=self.autoconnect_var, foreground='#0071ff') + self.autoconnect = ttk.Label(self.lb_frame3, textvariable=self.autoconnect_var, foreground='#0071ff', width=15) self.autoconnect.config(font=('Ubuntu', 11)) - self.autoconnect.grid(column=0, row=4, sticky='ne', pady=19) - - """ - Displays the value address, DNS and peer in the labels - or empty it again - """ + self.autoconnect.grid(column=1, row=0, sticky='e', pady=19) def init_and_report(self, data=None): + """ + Displays the value address, DNS and peer in the labels + or empty it again + """ + """ Address Label """ self.add = tk.StringVar() self.add.set('Address: ' + data[0]) @@ -974,6 +964,7 @@ class FrameWidgets(ttk.Frame): self.endpoint.config(font=('Ubuntu', 9)) def stop(self): + """ Stop Button """ self.btn_stst = ttk.Button(self.lb_frame_btn_lbox, image=self.wg_vpn_stop, command=self.wg_switch, padding=0) self.btn_stst.grid(column=0, row=0, padx=5, pady=8) @@ -991,6 +982,7 @@ class FrameWidgets(ttk.Frame): self.btn_stst.bind('', stop_leave) def start(self): + """ Start Button """ self.btn_stst = ttk.Button(self.lb_frame_btn_lbox, image=self.wg_vpn_start, command=self.wg_switch, padding=0) self.btn_stst.grid(column=0, row=0, padx=5, pady=8) @@ -1022,6 +1014,8 @@ class FrameWidgets(ttk.Frame): self.btn_stst.bind('', start_leave) def color_label(self): + """ View activ Tunnel in color green or yellow """ + with open(path_to_file2, 'r') as read_file: lines = read_file.readlines() if 'light\n' in lines: @@ -1034,6 +1028,7 @@ class FrameWidgets(ttk.Frame): self.lb_tunnel.grid(column=2, padx=10, row=1) def wg_switch(self): + self.a = Tunnel.active() try: if self.a == '':