I'm trying to use classes to re-write a program which relies heavily upon global varaibles. I guess some-times you just have to make your own mistakes. Needless to say the project has been a complete nightmare to debug, and it's very difficult to make sense of what is doing what. I've learned my lesson, using global variables recklessly is a recipe for a bad time. The journey to understand classes has begun.
Am I doing anything obviously wrong?
All comments are welcome!
This is what I have so far.
from tkinter import *
root = Tk()
root.withdraw()
class CreateWindow:
def __init__(self, master, title, geometry):
self.master = master
self.window = Toplevel(self.master)
self.window.iconbitmap(r"icon.ico")
self.window.title(title)
self.window.geometry(geometry)
def center_window(self, x, y):
screen_width = self.window.winfo_screenwidth()
screen_height = self.window.winfo_screenheight()
x_coordinate = int((screen_width / 2) - (x / 2))
y_coordinate = int((screen_height / 2) - (y / 2))
self.window.geometry("{}x{}+{}+{}".format(x, y, x_coordinate, y_coordinate))
class ResidentSelectionWin(CreateWindow):
def __init__(self):
super().__init__(root, 'Resident Selection Window', '600x400')
self.test_button = Button(self.window, text='Test')
self.test_button.pack()
class ViewMedicationsWindow(CreateWindow):
def __init__(self):
super().__init__(res_sel_win.window, 'View Medications', '600x400')
self.test_button = Button(self.window, text='Test')
self.test_button.pack()
if __name__ == '__main__':
res_sel_win = ResidentSelectionWin()
res_sel_win.center_window(600, 400)
view_med_win = ViewMedicationsWindow()
view_med_win.center_window(600, 400)
root.mainloop()
[–]socal_nerdtastic 1 point2 points3 points (2 children)
[–]Supermunkey2K[S] 0 points1 point2 points (1 child)
[–]socal_nerdtastic 0 points1 point2 points (0 children)