From 67f390cce23b01deb77ad18d5cae7d3cf4553fe0 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 17 Jun 2020 13:46:22 -0700 Subject: [PATCH] Move lines that modify parent out of the child class. Previous code was bad for modularity. Not that I'm using this as a module anywhere but it caught my attention so may as well fix it. --- PGUI.pyw | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/PGUI.pyw b/PGUI.pyw index d6e0809..0bb15d9 100644 --- a/PGUI.pyw +++ b/PGUI.pyw @@ -3,6 +3,7 @@ from tkinter import Tk, BOTH from tkinter.ttk import Frame, Style, Button import os import subprocess +import sys import winshell @@ -34,18 +35,7 @@ class PGUILauncher(Frame): Frame.__init__(self, parent) self.parent = parent - self.init_ui() - def launch_program(self, program): - print('opening application', program.name) - os.chdir(os.path.dirname(program.path)) - command = f'{program.path} {program.arguments}' - subprocess.Popen(command, shell=True) - self.quit() - - def init_ui(self): - self.parent.resizable(0,0) - self.parent.title("PGUI") self.style = Style() self.style.theme_use("clam") self.pack(fill=BOTH, expand = 1) @@ -57,7 +47,11 @@ class PGUILauncher(Frame): programs = load_programs() for (programindex, program) in enumerate(programs): print(y, x) - newButton = Button(self, text=program.name, command=lambda program=program: self.launch_program(program)) + newButton = Button( + self, + text=program.name, + command=lambda prog=program: self.launch_program(prog), + ) print(f'creating button for {program.name} at {program.path}') newButton.configure(width=self.buttonwidth) newButton.grid(row=y, column=x) @@ -70,17 +64,28 @@ class PGUILauncher(Frame): self.pack() self.update() - width = self.parent.winfo_reqwidth() - height = self.parent.winfo_reqheight() - x_offset = (self.parent.winfo_screenwidth() - width) / 2 - y_offset = (self.parent.winfo_screenheight() - height) / 2 + def launch_program(self, program): + print('opening application', program.name) + os.chdir(os.path.dirname(program.path)) + command = f'{program.path} {program.arguments}' + subprocess.Popen(command, shell=True) + self.quit() - self.parent.geometry('%dx%d+%d+%d' % (width, height, x_offset, y_offset-50)) -def main(): +def main(argv): root = Tk() + root.title("PGUI") + root.resizable(0,0) + ex = PGUILauncher(root) + + width = root.winfo_reqwidth() + height = root.winfo_reqheight() + x_offset = (root.winfo_screenwidth() - width) / 2 + y_offset = (root.winfo_screenheight() - height) / 2 + + root.geometry('%dx%d+%d+%d' % (width, height, x_offset, y_offset-50)) root.mainloop() if __name__ == '__main__': - main() + raise SystemExit(main(sys.argv[1:]))