Sof
Sof

Reputation: 59

change frame tkinter dynamically

I am designing a python game that requires a table, depending on the level the table must be 3x3, 4x4, or 5x5, Is there a WAY to change the frame size where I have the table dynamically?

I tried this but it doesn't work:

import tkinter.messagebox
from tkinter import *

size_of_board = IntVar
N = IntVar
symbol_X_color = '#EE4035'
symbol_O_color = '#0492CF'
Green_color = '#7BC043'


class Game():
    def __init__(self):
        global size_of_board
        size_of_board = 600

        def setLevelEasy():
            global N
            N = 2

        def setLevelMedium():
            global N, size_of_board
            N = 3
            size_of_board = 1
            self.initialize_board()

        self.root = Tk()
        self.root.geometry("1350x750+0+0")
        self.root.title("Game")
        self.root.configure(background='Teal')

        # MENU BAR
        my_menu = Menu(self.root)
        self.root.config(menu=my_menu)
        level_menu = Menu(my_menu, font=('Helvetica', 20, 'bold'))
        my_menu.add_cascade(label="Level", menu=level_menu)
        level_menu.add_command(label='Easy: 3x3', command=setLevelEasy)
        level_menu.add_command(label='Medium: 4x4', command=setLevelMedium)
        level_menu.add_command(label='Hard: 5x5')
        level_menu.add_command(label='Very Hard: 6x6')

        self.initialize_board()

        # TITLE
        Tops = Frame(self.root, bg='Teal', pady=2, width=1350, height=100, relief=RIDGE)
        Tops.grid(row=0, column=0)

        lblTitle = Label(Tops, font=('arial', 30, 'bold'), text="Game", bd=21, bg='Teal', fg='Cornsilk',
                         justify=CENTER)
        lblTitle.grid(row=1, column=0)

    def mainloop(self):
        self.root.mainloop()

    def initialize_board(self):
        global size_of_board
        print(size_of_board)

        MainFrame = Frame(self.root, bg='Turquoise', bd=10, width=size_of_board, height=size_of_board, relief=RIDGE)
        MainFrame.grid(row=1, column=0)


game_instance = Game()
game_instance.mainloop()

this is all my code, I want to resize the MainFrame on change level

Upvotes: 2

Views: 612

Answers (1)

Matiiss
Matiiss

Reputation: 6156

ok, so I made a sample from which You should have a rough idea of what to do:

from tkinter import Tk, Button


root = Tk()
list_of_names = []


def grid(n):
    global list_of_names
    cnt = 0
    for row in range(n):
        for col in range(n):
            button = Button(root, text=f'{list_of_names[cnt]}', width=6, height=3)
            button.grid(row=row, column=col)
            cnt += 1


three = Button(root, text='3x3', command=lambda: grid(3))
three.grid()
four = Button(root, text='4x4', command=lambda: grid(4))
four.grid()
five = Button(root, text='5x5', command=lambda: grid(5))
five.grid()

root.mainloop()

Upvotes: 1

Related Questions