Я нашел этот код онлайн в вопросе и реализовал его для своих собственных вещей, но я не мог понять, как это работает. Я разместил комментарии в тех местах, которые, как мне кажется, я знаю, что происходит, но если кто-нибудь сможет провести меня по областям, которые я выделил, это было бы удивительно.
class MyApp: # defines class
def __init__(self, main): # this function is run once an instance of the class is created
self.main_frame = main # creates a new frame called 'main_frame' and places it in the parent frame 'main'
tk.Grid.rowconfigure(self.main_frame, 0, weight=1) # not sure what this does
tk.Grid.columnconfigure(self.main_frame, 0, weight=1) # not sure what this does
self.checkbutton_frame = tk.Frame(self.main_frame) # creates a new frame called 'checkbutton_frame' and places it in the 'main_frame'
self.checkbutton_frame.grid(row=0, column=1)
self.button_frame = tk.Frame(self.main_frame) # creates a new frame called 'button_frame' and places it in the 'main_frame'
self.button_frame.grid(row=0, column=0, sticky='nsew') # not sure what this does
tk.Grid.rowconfigure(self.button_frame, 7, weight=1) # not sure what this does
tk.Grid.columnconfigure(self.button_frame, 0, weight=1) # not sure what this does
self.grid = tk.Frame(self.button_frame) # creates a new frame called 'grid' and places it in the 'button_frame'
self.grid.grid(sticky='nsew', column=0, row=7, columnspan=2) # not sure what this does
self.createbuttongrid() # calls the 'createbuttongrid' function
self.createcheckbuttons() # calls the 'createcheckbuttons' function
def createcheckbuttons(self):
for row in range(6):
checkbutton = tk.Checkbutton(self.checkbutton_frame, text='Checkbutton') # creates checkbuttons
checkbutton.grid(row=row, column=0) # places checkbuttons on rows/columns
def createbuttongrid(self):
for row in range(8):
for column in range(12):
button = tk.Button(self.button_frame, text='Button') # creates buttons
button.grid(row=row, column=column, sticky='nsew') # places buttons
for row in range(8):
tk.Grid.rowconfigure(self.button_frame, row, weight=1) # not sure what this does
for column in range(12):
tk.Grid.columnconfigure(self.button_frame, column, weight=1) # not sure what this does
if __name__ == "__main__":
import tkinter as tk # import statement
root = tk.Tk() # initializes the Tk window
MyApp(main=root) # tells the class that it should use this instance of the Tk() window. I can create multiple windows that interact separately by creating multiple tk.Tk()'s
root.mainloop() # forces the topmost window to stay open
Будет полезна любая помощь в понимании.