我在一个问题中在线找到了该代码,并将其实现为可以用于我自己的东西,但是我不知道它是如何工作的。我已经在我想知道发生了什么的地方发表评论,但是如果有人能够引导我浏览我所强调的地方,那就太好了。
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
任何对理解的帮助都会很棒。