- 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
This commit is contained in:
Désiré Werner Menrath 2024-11-08 20:49:59 +01:00
parent 80c63eaf78
commit bc0d46d633
3 changed files with 89 additions and 84 deletions

View File

@ -8,11 +8,21 @@ My standard System: Linux Mint 22 Cinnamon
- for loops with lists replaced by List Comprehensions - for loops with lists replaced by List Comprehensions
- Update search after start of Wire-Py - 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 ### Added
07-11-2024 07-11-2024
- remove classes and add methods to class FrameWidgets (removed self errors) - remove classes and add methods to class FrameWidgets (removed self errors)
### Added ### Added
27-10-2024 27-10-2024

View File

@ -14,7 +14,7 @@ import requests
''' 1 = 1. Year, 09 = Month of the Year, 2924 = Day and Year of the Year ''' ''' 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') path_to_file2 = Path('/etc/wire_py/settings.conf')
_u = Path.read_text(Path('/tmp/_u')) _u = Path.read_text(Path('/tmp/_u'))

View File

@ -5,8 +5,7 @@ import subprocess
import tkinter as tk import tkinter as tk
from pathlib import Path from pathlib import Path
from subprocess import check_call from subprocess import check_call
from tkinter import * from tkinter import filedialog, ttk, TclError
from tkinter import filedialog, ttk
from wg_func import (Tunnel, msg_window, WirePyUpdate, res, _u, version, path_to_file2, tips) 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.my_tool_tip = None
self.x_width = 600 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_x = self.winfo_screenwidth() / 2 - (self.x_width / 2)
self.monitor_center_y = self.winfo_screenheight() / 2 - (self.y_height / 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.title('Wire-Py')
self.geometry('%dx%d+%d+%d' % (self.x_width, self.y_height, self.monitor_center_x, self.monitor_center_y)) 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.style = ttk.Style(self)
self.tk.call('source', str(tcl_path) + '/water.tcl') self.tk.call('source', str(tcl_path) + '/water.tcl')
''' self.tk.call('source', 'TK-Themes/water.tcl') ''' ''' self.tk.call('source', 'TK-Themes/water.tcl') '''
@ -42,6 +44,28 @@ class MainWindow(tk.Tk):
''' Set it as the window icon ''' ''' Set it as the window icon '''
self.iconphoto(True, self.wg_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 ''' ''' Set on or off in file '''
def update(): def update():
@ -59,7 +83,7 @@ class MainWindow(tk.Tk):
with open(path_to_file2, 'w') as set_file2: with open(path_to_file2, 'w') as set_file2:
set_file2.writelines(lines2) set_file2.writelines(lines2)
''' Set on or off in file ''' ''' Set True or False in file '''
def tooltip(): def tooltip():
if set_tip.get(): if set_tip.get():
@ -87,6 +111,7 @@ class MainWindow(tk.Tk):
lines3[3] = 'light\n' lines3[3] = 'light\n'
with open(path_to_file2, 'w') as theme_set2: with open(path_to_file2, 'w') as theme_set2:
theme_set2.writelines(lines3) theme_set2.writelines(lines3)
self.color_label()
def theme_change_dark(): def theme_change_dark():
if not self.tk.call("ttk::style", "theme", "use") == "water-dark": if not self.tk.call("ttk::style", "theme", "use") == "water-dark":
@ -97,6 +122,7 @@ class MainWindow(tk.Tk):
lines4[3] = 'dark\n' lines4[3] = 'dark\n'
with open(path_to_file2, 'w') as theme_set2: with open(path_to_file2, 'w') as theme_set2:
theme_set2.writelines(lines4) theme_set2.writelines(lines4)
self.color_label()
def info(): def info():
def link_btn(): def link_btn():
@ -120,14 +146,12 @@ class MainWindow(tk.Tk):
''' Frame for Menu ''' ''' Frame for Menu '''
self.menu_frame = ttk.Frame(self) self.menu_frame = ttk.Frame(self)
self.menu_frame.configure(relief='flat') self.menu_frame.configure(relief='flat')
self.menu_frame.grid(column=0, row=0, sticky='w') self.menu_frame.grid(column=0, row=0, columnspan=4, sticky='w')
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
''' App Menu ''' ''' App Menu '''
self.version_lb = ttk.Label(self.menu_frame, text=version) self.version_lb = ttk.Label(self.menu_frame, text=version)
self.version_lb.config(font=('Ubuntu', 11), foreground='#00c4ff') 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): def version_enter(event):
""" The mouse moves into the Version widget """ """ The mouse moves into the Version widget """
@ -167,13 +191,11 @@ class MainWindow(tk.Tk):
''' About BTN Menu / Label ''' ''' About BTN Menu / Label '''
self.about_btn = ttk.Button(self.menu_frame, text='About', style='Toolbutton', command=info) self.about_btn = ttk.Button(self.menu_frame, text='About', style='Toolbutton', command=info)
self.about_btn.grid(column=2, row=0) self.about_btn.grid(column=2, row=0)
self.readme = tk.Menu(self) self.readme = tk.Menu(self)
''' Update and Tooltip Label ''' ''' Update and Tooltip Label '''
self.updates_lb = ttk.Label(self.menu_frame) self.updates_lb = ttk.Label(self.menu_frame)
self.updates_lb.grid(column=3, row=0, padx=10) self.updates_lb.grid(column=3, row=0, padx=10)
'''View Checkbox for enable or disable Tooltip ''' '''View Checkbox for enable or disable Tooltip '''
if tips: if tips:
set_tip.set(value=False) set_tip.set(value=False)
@ -237,29 +259,6 @@ class MainWindow(tk.Tk):
self.update_btn.configure(menu=self.download, style='Toolbutton') self.update_btn.configure(menu=self.download, style='Toolbutton')
self.download.add_command(label='Download', command=WirePyUpdate.download) 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 ''' ''' Show active Tunnel '''
self.a = Tunnel.active() 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 = ttk.Frame(self)
self.lb_frame_btn_lbox.configure(relief='flat') self.lb_frame_btn_lbox.configure(relief='flat')
self.lb_frame_btn_lbox.grid(column=0, rowspan=3, row=1) 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 ''' ''' Label Frame 2 '''
self.lb_frame = ttk.Frame(self) self.lb_frame = ttk.Frame(self)
self.lb_frame.configure(relief='solid') self.lb_frame.configure(relief='solid')
self.lb_frame.grid(column=2, row=2, sticky='snew', padx=20, pady=5) 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 ''' ''' Label Frame 3 '''
self.lb_frame2 = ttk.Frame(self) self.lb_frame2 = ttk.Frame(self)
self.lb_frame2.configure(relief='solid') self.lb_frame2.configure(relief='solid')
self.lb_frame2.grid(column=2, row=3, sticky='snew', padx=20, pady=5) 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 ''' ''' Show active Label '''
self.select_tunnel = None self.select_tunnel = None
self.lb = ttk.Label(self, text='Active: ') self.lb = ttk.Label(self, text='Active: ')
self.lb.config(font=('Ubuntu', 11, 'bold')) self.lb.config(font=('Ubuntu', 11, 'bold'))
self.lb.grid(column=2, row=1, padx=15, pady=4, sticky='w') 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 ''' ''' Label to Show active Tunnel '''
self.StrVar = tk.StringVar(value=self.a) 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 = 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.scrollbar.grid(column=1, rowspan=4, row=0, sticky='nse')
self.l_box.configure(yscrollcommand=self.scrollbar.set) self.l_box.configure(yscrollcommand=self.scrollbar.set)
self.rowconfigure(0, weight=1)
''' Tunnel List ''' ''' Tunnel List '''
self.tl = Tunnel.list() self.tl = Tunnel.list()
@ -546,8 +541,8 @@ class FrameWidgets(ttk.Frame):
self.btn_exp.bind('<Leave>', exp_leave) self.btn_exp.bind('<Leave>', exp_leave)
''' Label Entry ''' ''' Label Entry '''
self.lb_rename = ttk.Entry(self, width=20) self.lb_rename = ttk.Entry(self.lb_frame3, width=20)
self.lb_rename.grid(column=2, row=4, padx=30, pady=15, sticky='nw') 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.insert(0, 'Max. 12 characters!')
self.lb_rename.config(state='disable') self.lb_rename.config(state='disable')
@ -647,21 +642,21 @@ class FrameWidgets(ttk.Frame):
msg_window(iw, ii, wt, msg_t) msg_window(iw, ii, wt, msg_t)
''' Button Rename ''' ''' 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') 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 ''' ''' Check Buttons '''
self.selected_option = tk.IntVar() self.selected_option = tk.IntVar()
self.autoconnect_var = tk.StringVar() self.autoconnect_var = tk.StringVar()
self.autoconnect_var.set(self.auto_con) self.autoconnect_var.set(self.auto_con)
''' Frame for Labels, Entry and Button''' ''' 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.config(font=('Ubuntu', 11))
self.autoconnect.grid(column=0, row=4, sticky='ne', padx=10, pady=15) self.autoconnect.grid(column=1, row=0, sticky='e', pady=19)
self.wg_autostart = ttk.Checkbutton(self, text='Autoconnect on:', variable=self.selected_option, self.wg_autostart = ttk.Checkbutton(self.lb_frame3, text='Autoconnect on:', variable=self.selected_option,
command=self.box_set) 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): def chk_enter(event):
""" The mouse moves into the entry widget """ """ The mouse moves into the entry widget """
@ -707,19 +702,17 @@ class FrameWidgets(ttk.Frame):
self.on_off() 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): 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: try:
filepath = filedialog.askopenfilename(initialdir=str(_u), title='Select Wireguard config File', filepath = filedialog.askopenfilename(initialdir=str(_u), title='Select Wireguard config File',
filetypes=[('WG config files', '*.conf')], ) filetypes=[('WG config files', '*.conf')], )
@ -875,14 +868,12 @@ class FrameWidgets(ttk.Frame):
print('Tunnel exist!') 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): 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: try:
select_tunnel = self.l_box.curselection() select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(select_tunnel[0]) select_tl = self.l_box.get(select_tunnel[0])
@ -911,13 +902,12 @@ class FrameWidgets(ttk.Frame):
self.on_off() 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): 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: with open(path_to_file2, 'r') as set_file4:
lines4 = set_file4.readlines() lines4 = set_file4.readlines()
@ -934,16 +924,16 @@ class FrameWidgets(ttk.Frame):
self.autoconnect_var = tk.StringVar() self.autoconnect_var = tk.StringVar()
self.autoconnect_var.set(self.auto_con) 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.config(font=('Ubuntu', 11))
self.autoconnect.grid(column=0, row=4, sticky='ne', pady=19) self.autoconnect.grid(column=1, row=0, sticky='e', pady=19)
"""
Displays the value address, DNS and peer in the labels
or empty it again
"""
def init_and_report(self, data=None): def init_and_report(self, data=None):
"""
Displays the value address, DNS and peer in the labels
or empty it again
"""
""" Address Label """ """ Address Label """
self.add = tk.StringVar() self.add = tk.StringVar()
self.add.set('Address: ' + data[0]) self.add.set('Address: ' + data[0])
@ -974,6 +964,7 @@ class FrameWidgets(ttk.Frame):
self.endpoint.config(font=('Ubuntu', 9)) self.endpoint.config(font=('Ubuntu', 9))
def stop(self): 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 = 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) self.btn_stst.grid(column=0, row=0, padx=5, pady=8)
@ -991,6 +982,7 @@ class FrameWidgets(ttk.Frame):
self.btn_stst.bind('<Leave>', stop_leave) self.btn_stst.bind('<Leave>', stop_leave)
def start(self): 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 = 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) self.btn_stst.grid(column=0, row=0, padx=5, pady=8)
@ -1022,6 +1014,8 @@ class FrameWidgets(ttk.Frame):
self.btn_stst.bind('<Leave>', start_leave) self.btn_stst.bind('<Leave>', start_leave)
def color_label(self): def color_label(self):
""" View activ Tunnel in color green or yellow """
with open(path_to_file2, 'r') as read_file: with open(path_to_file2, 'r') as read_file:
lines = read_file.readlines() lines = read_file.readlines()
if 'light\n' in lines: if 'light\n' in lines:
@ -1034,6 +1028,7 @@ class FrameWidgets(ttk.Frame):
self.lb_tunnel.grid(column=2, padx=10, row=1) self.lb_tunnel.grid(column=2, padx=10, row=1)
def wg_switch(self): def wg_switch(self):
self.a = Tunnel.active() self.a = Tunnel.active()
try: try:
if self.a == '': if self.a == '':