little fixes, add msg_window()

function for Messagebox to show a tk.Toplevel()
replace var = open() with:
with open() as var:
and remove by classes (tk.tk) and super()
This commit is contained in:
2024-09-14 23:25:48 +02:00
parent 8fa6e6374f
commit d1a822fff0
5 changed files with 115 additions and 111 deletions

View File

@ -7,39 +7,37 @@ import tkinter as tk
from pathlib import Path
from subprocess import check_call
home = Path.home()
font_color = '#4011a7'
path_to_file = home / '.config/wire_py/wg_py'
path_to_file = Path.home() / '.config/wire_py/wg_py'
class Message(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.warning_pic = None
self.x_width = 300
self.y_height = 120
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.title('Import error!')
self.configure()
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.label = tk.Label(self,
image=self.warning_pic,
text='Oh... no valid Wireguard File!\nPlease select a valid Wireguard File')
self.label.config(font=('Ubuntu', 11), padx=15, pady=15)
self.label.grid(column=0, row=0)
self.button = tk.Button(self, text='OK', command=self.destroy)
self.button.config(padx=15, pady=5)
self.button.grid(column=0, row=1)
def msg_window():
msg = tk.Toplevel()
msg.resizable(width=False, height=False)
msg.x_width = 340
msg.y_height = 140
msg.title('Import error!')
msg.monitor_center_x = msg.winfo_screenwidth() / 2 - (msg.x_width / 2)
msg.monitor_center_y = msg.winfo_screenheight() / 2 - (msg.y_height / 2)
msg.geometry('%dx%d+%d+%d' % (msg.x_width, msg.y_height, msg.monitor_center_x, msg.monitor_center_y))
msg.columnconfigure(0, weight=1)
msg.configure(pady=20)
msg.warning = tk.PhotoImage(file=r'icons/warning_64.png')
msg.i_warning = tk.Label(msg, image=msg.warning)
msg.i_warning.grid(column=0, row=0)
label = tk.Label(msg, text='Oh... no valid Wireguard File!\nPlease select a valid Wireguard File')
label.config(font=('Ubuntu', 11), padx=15, pady=15)
label.grid(column=1, row=0)
button = tk.Button(msg, text='OK', command=msg.destroy)
button.config(padx=15, pady=5)
button.grid(column=0, columnspan=2, row=1)
wg_icon_2 = tk.PhotoImage(file=r'icons/wg-stop.png')
msg.iconphoto(True, wg_icon_2)
msg.winfo_toplevel()
class GreenLabel(tk.Tk):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
class GreenLabel:
def __init__(self):
self.StrVar = None
self.lb_tunnel = None
@ -50,10 +48,15 @@ class GreenLabel(tk.Tk):
self.columnconfigure(2, weight=1)
self.rowconfigure(0, weight=1)
def columnconfigure(self, param, weight):
pass
class StartStopBTN(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def rowconfigure(self, param, weight):
pass
class StartStopBTN:
def __init__(self):
self.lb_frame_btn_lbox = None
self.wg_switch = None
self.btn_stst = None
@ -116,9 +119,8 @@ class TunnelActiv:
return active
class ShowAddress(tk.Tk):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
class ShowAddress:
def __init__(self):
self.lb_frame2 = None
self.lb_frame = None
self.endpoint = None
@ -165,9 +167,9 @@ class ListTunnels:
return tl
class ImportTunnel(tk.Tk):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
class ImportTunnel:
def __init__(self):
self.select_tunnel = None
self.wg_switch = None
@ -179,27 +181,26 @@ class ImportTunnel(tk.Tk):
def wg_import_select(self):
try:
filepath = filedialog.askopenfilename(initialdir=home, title='Select Wireguard config File',
filetypes=[('WG config files', '*.conf')])
file = open(filepath, 'r')
read = file.read()
file.close()
filepath = filedialog.askopenfilename(initialdir=Path.home(), title='Select Wireguard config File',
filetypes=[('WG config files', '*.conf')], )
with open(filepath, 'r') as file:
read = file.read()
path_split = filepath.split('/')
path_split1 = path_split[-1]
self.a = TunnelActiv.active()
if 'PrivateKey = ' in read and 'PublicKey = ' in read:
if len(path_split1) > 17:
p1 = shutil.copy(filepath, home / 'tester/')
p1 = shutil.copy(filepath, Path.home() / 'tester/')
path_split = path_split1[len(path_split1) - 17:]
os.rename(p1, home / 'tester/' / str(path_split))
os.rename(p1, Path.home() / 'tester/' / str(path_split))
if self.a != '':
check_call(['nmcli', 'connection', 'down', TunnelActiv.active()])
ShowAddress.label_empty(self)
check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', home / 'tester' /
check_call(['nmcli', 'connection', 'import', 'type', 'wireguard', 'file', Path.home() / 'tester' /
path_split])
else:
shutil.copy(filepath, home / 'tester/')
shutil.copy(filepath, Path.home() / 'tester/')
if self.a != '':
check_call(['nmcli', 'connection', 'down', TunnelActiv.active()])
ShowAddress.label_empty(self)
@ -213,16 +214,15 @@ class ImportTunnel(tk.Tk):
self.StrVar.set(self.a)
GreenLabel.green_show_label(self)
StartStopBTN.button_stop(self)
wg_read = home / 'tester/' / str(self.a + '.conf')
wg_read = Path.home() / 'tester/' / str(self.a + '.conf')
with open(wg_read, 'r') as file:
data = ConToDict.covert_to_dict(file)
# Address Label
ShowAddress.init_and_report(self, data)
ShowAddress.show_data(self)
file.close()
check_call(['nmcli', 'con', 'mod', self.a, 'connection.autoconnect', 'no'])
if 'PrivateKey = ' not in read:
Message()
msg_window()
except EOFError:
pass
except TypeError:
@ -242,23 +242,20 @@ class FileHandle:
self.tl = None
self.selected_option = None
self.l_box = None
self.select_tunnel = None
def box_set(self):
try:
self.select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(self.select_tunnel[0])
select_tunnel = self.l_box.curselection()
select_tl = self.l_box.get(select_tunnel[0])
if self.selected_option.get() == 0:
off = open(path_to_file, 'w')
off.write('false ' + '\n')
off.close()
with open(path_to_file, 'w') as off:
off.write('false ' + '\n')
tl = ListTunnels.tl_list()
if len(tl) == 0:
self.wg_autostart.configure(state='disabled')
if self.selected_option.get() >= 1:
set_on = open(path_to_file, 'w')
set_on.write('true ' + select_tl)
set_on.close()
with open(path_to_file, 'w') as set_on:
set_on.write('true ' + select_tl)
except IndexError:
self.selected_option.set(1)
@ -266,9 +263,13 @@ class FileHandle:
OnOff.on_off(self)
class OnOff(tk.Tk, FileHandle):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
class OnOff:
def __init__(self):
self.wg_autostart = None
self.selected_option = None
self.auto_con = None
self.autoconnect = None
self.autoconnect_var = None
self.lb_frame_buttons = None
def on_off(self):
@ -282,7 +283,6 @@ class OnOff(tk.Tk, FileHandle):
else:
self.wg_autostart.configure(state='disabled')
self.auto_con = 'no Autoconnect'
file.close()
self.autoconnect_var = tk.StringVar()
self.autoconnect_var.set(self.auto_con)
@ -299,8 +299,8 @@ class ExportTunnels:
tl = ListTunnels.tl_list()
try:
if len(tl) != 0:
wg_tar = home / now_datetime
p_to_conf = home / 'tester/'
wg_tar = Path.home() / now_datetime
p_to_conf = Path.home() / 'tester/'
shutil.make_archive(wg_tar, 'zip', p_to_conf)
#if zip_full != 0:
#print('Export erfolgraeich')