Cant figure out how to stop columns and rows from shifting in TkInter GUI Cant figure out how to stop columns and rows from shifting in TkInter GUI tkinter tkinter

Cant figure out how to stop columns and rows from shifting in TkInter GUI


A minimum size for each grid cell can be set with the function .minsize. If you set a minimum size that's always larger than the widgets you put into each cell, the cells will not change size as you move things around. See this manual page: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-config.html.


Well there are a few issues with your code if you are working on Python 3.X.

  • The first problem is that you are trying to use a .png file whereas the PhotoImage class only accepts .gif, .pgm, or .ppm files. You can read the doc at PhotoImage Doc.
  • The next issue, a warning, is that you are setting variables as global at the end of your functions. The way python works is that if a variable is declared inside a function it is local, so what you normally do is tell it at the start of the function, ok this variable is global, and THEN proceed to modifying it. If not you can't be sure that the variable you are working on is the global one you want.
  • For the grid resizing, your best option is to put the buttons inside a grid, inside the grid so that their size is independent of the size of cells in the main grid. And it is better to display things on a frame rather than directly on master. I would change your code to be something like (and replace Labels and Frames from functions to construct on master to construct on gridFrame):

    WIDTH = 200HEIGHT = 200master= Tk()master.resizable(width=False, height=False)gridFrame = Frame(master, width=WIDTH, height=HEIGHT)gridFrame.grid()# Your function code goes herebuttonGrid = Frame(gridFrame)buttonGrid.grid(row=4, column=3, sticky=N)# created widgetslabel1 = Label(gridFrame, text="Enter a command:")command = Entry(gridFrame, width=80)leftButton = Button(buttonGrid, text="<", command=left)rightButton = Button(buttonGrid, text=">", command=right)downButton = Button(buttonGrid, text="v", command=down)upButton = Button(buttonGrid, text="^", command=up)submitButton = Button(gridFrame, text="SUBMIT", command=submit)# display widgetslabel1.grid(row=5, column=1, sticky=E)command.grid(row=5, column=2)leftButton.grid(row=1, column=0, sticky=E)rightButton.grid(row=1, column=2, sticky=W)downButton.grid(row=2, column=1, sticky=N)upButton.grid(row=0, column=1, sticky=S)submitButton.grid(row=5, column=3, pady=5, padx=5)
  • The bit about knowing where something is can be easily maintainted by simply having a matrix that is initially empty and when you place something at a certain row and column, change those indices of the matrix to True and then simply check the matrix at position [i][j] to see if it is occupied

Hope that helped.