trouble converting a resizing button grid from tkinter into custom tkinter

  Kiến thức lập trình

So i have been trying to make a grid of buttons that resizes to the window size. I managed to find a solution in tkinter which I tried and it worked but as soon as i tried to use the equivalent customtkinter code It is not working as intended anymore
the regular tkinter code I used is as follows

def openWindow(self):
            if self.txtboxWidth.get() != "" and self.txtboxHeight != "":        
                try:
                    self.gridWidth = int(self.txtboxWidth.get())
                    self.gridHeight = int(self.txtboxHeight.get())
                    self.newWindow = tk.Toplevel(self.master)
                    print("TRUE")
                    self.app = mapMaker(self.newWindow, (self.gridWidth, self.gridHeight))
                    self.frame.destroy()
                except:
                    mb.showerror(title="CALCULATION ERROR", message="PLEASE MAKE SURE ALL INPUTS ARE OF INT TYPE AND THAT ALL BOXES ARE FILLED")
            else:
                self.newWindow = tk.Toplevel(self.master)
                self.app = mapMaker(self.newWindow)
                self.frame.destroy()
class mapMaker:
    def __init__(self, master, grid_size = GRID_SIZE):
        super().__init__()
        self.master = master
        self.gridsize = grid_size
        self.doorLocation = ()

        if self.gridsize != GRID_SIZE:
            self.gridsize = (round_up_to_BOXSIZE(self.gridsize[0]) // BOX_SIZE, round_up_to_BOXSIZE(self.gridsize[1]) // BOX_SIZE)
            
        self.colours = ['white', 'red', 'blue']
        self.gridValues = ['', 'WALL', 'DOOR']

        tk.Grid.rowconfigure(self.master, 0, weight = 1)
        tk.Grid.columnconfigure(self.master, 0, weight=1)
        
        self.frame = ttk.Frame(self.master)
        self.frame.grid(row= 0, column=0, sticky="news")

        self.buttons = {}
        for row_index in range(self.gridsize[1]):
            tk.Grid.rowconfigure(self.frame, row_index, weight = 1)
            for col_index in range(self.gridsize[0]):
                tk.Grid.columnconfigure(self.frame, col_index, weight = 1)
                btn = tk.Button(self.frame, text= '',
                                command = lambda row = row_index, column = col_index: self.onClick(row, column),
                                bg = "white")
                self.buttons[(row_index, col_index)] = btn
                btn.grid(row=row_index, column=col_index, sticky="news")

    def onClick(self, row, column):
        new_colour = nextInList(self.colours, self.buttons[(row, column)]['bg'])
        value = self.colours.index(new_colour)
        if new_colour == 'blue':
            if self.doorLocation:
                self.buttons[(self.doorLocation[0], self.doorLocation[1])].config(bg = 'red')
                self.buttons[(self.doorLocation[0], self.doorLocation[1])].config(text = 'WALL')
                self.doorLocation = (row, column)
            else:   
                self.doorLocation = (row, column)
        if new_colour == 'white':
            self.doorLocation = ()
            
        self.buttons[(row, column)].config(bg = new_colour)
        self.buttons[(row, column)].config(text = self.gridValues[value])
def validate_form(self):
        if self.widthEntry.get() != "" or self.heightEntry.get() != "":
            try:
                self.gridWidth = int(self.widthEntry.get())
                self.gridHeight = int(self.heightEntry.get())
                self.newWindow = ctk.CTkToplevel(self.master)
                self.app = MapMaker(self.newWindow, (self.gridWidth, self.gridHeight))
                self.frame.destroy()
            except:
                mb.showerror(title="INPUT ERROR", message="MAKE SURE THAT BOTH FIELDS ARE EITHER FILLED OR EMPTY AND OF THE CORRECT DATA TYPE")
        else:
            self.newWindow = ctk.CTkToplevel(self.master)
            self.app = MapMaker(self.newWindow)
            self.frame.destroy()
class MapMaker:
    def __init__(self, master, grid_size = GRIDSIZE):
        super().__init__()
        self.master = master
        self.gridsize = grid_size 

        self.frame = ttk.Frame(self.master)
        self.frame.grid(row = 0, column = 0, sticky= "news")


        self.master.grid_rowconfigure(0, weight = 1)
        self.master.grid_columnconfigure(0, weight = 1)


        for row in range(self.gridsize[1]):
            self.frame.grid_rowconfigure(row, weight = 1)
            for column in range(self.gridsize[0]):
                self.frame.grid_columnconfigure(column, weight = 1)
                btn = ctk.CTkButton(self.frame, text = '',)
                btn.grid(row = row, column = column, sticky = "news")

this is what i got when i run this (Only the green bits are clickable)

New contributor

Gnomithy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT