Python Forum
Not understanding the correlation between code and geometry with Tkninter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Not understanding the correlation between code and geometry with Tkninter (/thread-11976.html)



Not understanding the correlation between code and geometry with Tkninter - Intelligent_Agent0 - Aug-03-2018

Hello, I recently started working on a personal app to help me learn GUI programming with python Tkinter. What I am having trouble with is coding the geometry, I learned how to make buttons and labels, but when I run my program I have no control over where they will be displayed within the screen. Here is an example program I wrote, It is to become an online shopping app:
from tkinter import *

root = Tk()

BeveragesButton = Button(master = root, bg = "white", fg = "green", text = "Beverages")
Meat_SeafoodButton = Button(master = root, bg = "white", fg = "green", text = "Meat & Seafood")
BakeryButton = Button(master = root, bg = "white", fg = "green", text = "Bakery")
PantryButton = Button(master = root, bg = "white", fg = "green", text = "Pantry")
Snacks_SweetsButton = Button(master = root, bg = "white", fg = "green", text = "Snacks & Sweets")
Dairy_EggsButton = Button(master = root, bg = "white", fg = "green", text = " Dairy & Eggs")
Frozen_Foods = Button(master = root, bg = "white", fg = "green", text = "Frozen Foods")
ButtonsList1 = [BeveragesButton,Meat_SeafoodButton,BakeryButton,PantryButton,Snacks_SweetsButton,Dairy_EggsButton,Frozen_Foods]

listbox = Listbox(root)
listbox.pack(fill = BOTH,expand = 1)

for button in ButtonsList1:
	listbox.insert(END)
	button.pack()

mainloop()
This is a image of the window python produces from the code:

NOTE: I thought I could show you a picture but I guess this forum doesn't support that.

Anyhow, the problem is, when I run the code, the buttons appear in the bottom center under a large white space. I want the buttons lined up from top to bottom on left side of screen. What can I do to fix my geometry problems?


RE: Not understanding the correlation between code and geometry with Tkninter - Larz60+ - Aug-03-2018

Quote:What I am having trouble with is coding the geometry,
tkinters geometry is the main reason I stopped using it, and switched to wxpython phoenix (for python 3+),

pack is ok for very simple applications. It quickly becomes unruly when you have more than a few frames.
In my opinion, You don't really have a chance without using grid.

Tkinter's resizing is possible using weight, but still a pain to get right.

The best write-up I have found on this is: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/root-resize.html
This document is the best I have found for tkinter.

Just an FYI, wxpython's geometry is a snap, including dockable windows, etc. It might be worth taking a look at.
Here's a sample widget page: https://wxpython.org/Phoenix/docs/html/gallery.html


RE: Not understanding the correlation between code and geometry with Tkninter - Grytpype - Aug-03-2018

The large white space is your listbox. It contains seven empty lines, one for each time you went round the loop. Listboxes contain text: listbox.insert(END, "hyena"). It looks as if you want to insert the buttons in the listbox. But the loop alternately adds an empty line to the listbox and packs a button below it.

I agree that grid() gives you much better control than pack().


RE: Not understanding the correlation between code and geometry with Tkninter - Axel_Erfurt - Aug-04-2018

Buttons left

for button in ButtonsList1:
    listbox.insert(END)
    button.pack(anchor = 'w')