So I'm trying to create a simple game, and right now the interface for the game requires a grid of buttons to be created based on a number required by the user. Currently I have a scale widget which a allows a user to choose a grid size, and a button which when clicked, opens another window with a grid of buttons created. The size of the grid depends on where the scale slider is positioned.

Now, I would like to make this grid of buttons centered everytime the window is opened. Do I need to put the buttons inside a frame or something? I'm not sure about this so I would need help on this. My code is posted below. The button grid was created using the code from this thread: http://www.daniweb.com/software-development/python/threads/240519/getting-text-values-from-buttons-in-tkinter-to-make-a-version-of-minesweeper

from Tkinter import *

buttons = {}

def makeChoice(event):
    global buttons
    print buttons[event.widget]

def gameWindow():
    a = size.get()
    main = Tk()
    main.wm_title("My Game")
    buttonNum = 0
    x = 0
    y = 0    
    for i in range(a + 1):
        for j in range(a):
            button = Button(main, text = " ", font = "Courier 9", width = 2, bd = 2)
            button.place(relx = 1, x = x, y = y) 
            buttons[button] = buttonNum
            buttonNum += 1
            button.bind("<Button-1>", makeChoice)
            x += 25
        x = -150
        y += 25            

window = Tk()
window.wm_title("My Game")

canvas = Canvas(window, width = 350, height = 278)
mainimg = PhotoImage(file = '1.gif')
canvas.pack(expand = NO, fill = BOTH)
canvas.create_image(200, 30, image = mainimg, anchor = N)

lblmain = Label(window, text = "Choose a size to begin.")
lblmain.pack()

size = IntVar()
size.set(4)
slider = Scale(window, orient="horiz", from_=4, to=9, variable=size)
slider.pack()

btnstart = Button(window, text = "Start Game", command = gameWindow)
btnstart.pack()

window.geometry("%dx%d%+d%+d" % (400, 400, 0, 0))
window.resizable(0,0)

f = Frame(window)
f.pack()

window.mainloop()

Recommended Answers

All 2 Replies

all what i can say that use PyQt it is better i don't know whats your problem is but that what i can say :\

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.