简体   繁体   中英

Python tkinter main window improper size when .grid() widgets

I have a game board which is rows x columns list.

Min size is 2x2 and max 10x10, with unequal rows:columns being okay (eg 2x3, 4x9).

Main window object has no predetermines geometry size setting, and widgets (buttons) are being .grid() in it for each list element in a was that creates a 2D map.

Ideally, given the method used this would lead to a nice, edge=to-edge map inside the main window.

Unfortunately, testing has shown that while this is true for maps with columns count > 3, when columns <= 3 then the window seems to default to a certain X-size, where this ugly free space is present at the right of the window. This is not the case for Y-axis, which is defined by rows.

Note that buttons placed are fixed 32x32 px (determined by image inside).

def createMap (): #creates rows x columns 2D list - a map
    global rowsEntryVar, columnsEntryVar, mapList
    mapList = []
    for row in range(rowsEntryVar):
        tempList = []
        for column in range(columnsEntryVar):
            tempList.append(Button(root, bd=0, bg=redMagenta, activebackground=redMagenta))
        mapList.append(tempList)

and then:

def drawMap ():
    global mapList
    for row in range(len(mapList)):
        for column in range(len(mapList[row])):
            mapList[row][column].grid(row=row, column=column)

Image:

Image showing the problem

Please go easy on me, I'm quite new to programming. :)

when columns <= 3 then the window seems to default to a certain X-size,

Tkinter defaults to the size of the widgets so you must be setting the geometry for "root" somewhere. The following works fine on my Slackware box (and using a function as a function eliminates the globals). If you are just starting, then it is good to form good habits, like conforming to the Python Style Guide https://www.python.org/dev/peps/pep-0008/ (variables and functions are all lower case with underlines).

from Tkinter import *

def create_map (rowsEntryVar, columnsEntryVar): #creates rows x columns 2D list - a map
    mapList = []
    for row in range(rowsEntryVar):
        tempList = []
        for column in range(columnsEntryVar):
            tempList.append(Button(root, text="%s-%s" % (row, column),
                            bd=0, bg="magenta2", activebackground=r"magenta3"))
        mapList.append(tempList)
    return mapList

def draw_map(mapList):
    for row in range(len(mapList)):
        for column in range(len(mapList[row])):
            mapList[row][column].grid(row=row, column=column)


root = Tk()
map_list=create_map(4, 3)
draw_map(map_list)
root.mainloop()

This appears to be a platform-specific limitation. I can't duplicate the problem on my Mac, but I can on a windows VM. Apparently, Windows won't allow the width of the window to be smaller than the space required for the buttons and icon on the titlebar.

My advice is to give the rows and columns a positive weight so that they will grow to fit the window, and then use the sticky option to cause the buttons to fill the space given to them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM