Python Forum
Not understanding the correlation between code and geometry with Tkninter
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not understanding the correlation between code and geometry with Tkninter
#1
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?
Reply
#2
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/tk...esize.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
Reply
#3
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().
Reply
#4
Buttons left

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner question re: Tkinter geometry return2sender 3 904 Jun-19-2023, 06:19 PM
Last Post: deanhystad
Question [Tkinter] How to change geometry? Meffy 4 2,839 Jan-31-2021, 10:35 AM
Last Post: Meffy
  [Tkinter] Window geometry appears different on Win and Linux steve_shambles 6 6,935 Nov-29-2019, 12:30 AM
Last Post: steve_shambles
  Grid geometry doesnt work as expected Sigmiami 3 3,753 Oct-25-2018, 04:56 PM
Last Post: The_Raven
  [Tkinter] What is Controller in this code i am not understanding what is it Rishav 2 7,289 Jul-10-2017, 06:36 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020