Python Forum
[Tkinter] how do i add a margin to a Listbox - 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: [Tkinter] how do i add a margin to a Listbox (/thread-17164.html)

Pages: 1 2 3


how do i add a margin to a Listbox - DT2000 - Mar-31-2019

I have been trying to find information to help me margin (pad) the Listbox when it is populated from a database query. Currently the inserted data is right against the Listbox frame and it can make it hard to read.
Using the grid method I know it can be done by "name.grid(ipadx and ipady)", however I have been writing this project using the pack method.

Can a Listbox have a margin of 1 or 2 character spaces when the data is inserted so it is not against the frame?
output = tkinter.Listbox(window_2, height = 20, font='Times 10',
width=42, bd=1, bg = '#FFD599', fg = '#9A0615', selectmode=SINGLE)
output.pack()
output.place(x=210, y=195)
yscroll1 = tkinter.Scrollbar(command=output.yview,orient=tkinter.VERTICAL)
yscroll1.place(x=463, y=196)
output.configure(yscrollcommand=yscroll1.set)
output.bind('<ButtonRelease-1>', getRecipe)



RE: how do i add a margin to a Listbox - Larz60+ - Mar-31-2019

1. You cannot use grid place and pack together for widgets inside of a container.
This means that for window2, you can choose grid, place or pack, but no more than one.
for margins, use ipadx, ipady, padx, pady
In tkinter, not all widgets support this, and methods are inconsistent between different geometries (my pet peeve with tkinter)


RE: how do i add a margin to a Listbox - DT2000 - Apr-01-2019

Thanks Larz... I have been using the pack method in this project as you know. I guess I would have to do a small rewrite and use the grid method instead in order to add the margin (padding) inside the Listbox.
(on a small note, you have linked this thread in your reply)


RE: how do i add a margin to a Listbox - Larz60+ - Apr-01-2019

link removed, thanks for pointing that out with the number of posts we review in a day, that happens.

Quote: I have been using the pack method in this project as you know.
Yes, You have been using pack, but have also illegally been using place, choose one or the other.
output.pack()
output.place(x=210, y=195)
you don't have to use grid. Pack also has padding attributes. See the docs.
Though older, tkinter hasen't changed, so this is still the best (In My Opinion): http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html

pack is easier to use than grid, grid however gives you more control at a higher frustration level.
This is, in my opinion a huge pain in the butt, and why I prefer wxpython (phoenix, python 3) five times over tkinter.


RE: how do i add a margin to a Listbox - wuf - Apr-01-2019

Hi DT2000

Put your listbox in a Frame and add with padx & pady some border to the listbox. For instance:
import tkinter as tk

class Application(object):
    INTERIOR_BORDER = 10
    
    def __init__(self, mainm_win):
        self.main_win = main_win
        
        self.build()
    
    def build(self):
        main_frame = tk.Frame(self.main_win, relief='sunken', bd=1, bg='white')
        main_frame.pack(fill='both', expand=True, padx=5, pady=5)
        main_frame.columnconfigure(0, weight=1)
        main_frame.rowconfigure(0, weight=1)
        
        list_item_values = ["one", "two", "three", "four", "five"]*5
        
        listbox = tk.Listbox(main_frame, width=42, height=20, bd=0,
            highlightthickness=0)
        listbox.grid(row=0, column=0, padx=self.INTERIOR_BORDER,
            pady=self.INTERIOR_BORDER, sticky='news')
        listbox.insert('end', *list_item_values)

        yscroll1 = tk.Scrollbar(main_frame, command=listbox.yview,
            orient=tk.VERTICAL)
        yscroll1.grid(row=0, column=1, sticky='ns')
        
        listbox.configure(yscrollcommand=yscroll1.set)
    
        listbox.bind('<ButtonRelease-1>', self.get_recipe)
        
    def get_recipe(self, event=None):
        print("Get Recipe")
        
if __name__ == "__main__":
    main_win = tk.Tk()
    main_win.title("Listbox with internal border")
    app = Application(main_win)
    main_win.mainloop()
Greetings wuf :-)


RE: how do i add a margin to a Listbox - DT2000 - Apr-01-2019

Thanks again Larz, I have been using the pack method in this project but it does not allow me to margin the inside of the Listbox so when data is inserted from the database query it can be more easily read whereas the grid method allows ipadx and ipady to margin the inside of the box.
I have written a small code using the grid method a couple months ago to try it but it as you said a little more frustrating at times.
I will look at wxpython this evening and see if I can get used to it. I'm still quite new to python and I have only been using tkinter.


wuf, I have looked at the example you have provided(thank you), but the problem is that it cannot be used because it is using the grid method whereas I have written this piece using the pack method so there is a conflict in the geometry manager when grid is attempted.


RE: how do i add a margin to a Listbox - Larz60+ - Apr-01-2019

you can also add spaces before and after the text that you send to the Listbox, it's a pain but it works.
If you ever want to look at wxpython, you can install with
pip install wxpython
then download the demo here: https://github.com/wxWidgets/Phoenix/tree/master/demo


RE: how do i add a margin to a Listbox - DT2000 - Apr-01-2019

I installed wxpython directly after my last reply. I plan to look at it closer this evening. I'm not sure how much of a learning curve it is compared to tkinter but I will certainly give it a try.

Adding at least 1 character space in the Listbox is exactly what I have been trying do so the inserted data would be easier to read, but without success so far using pack method.

I was also suggested to use the following in the query and insert:
output.delete(0, END)
selection = var.get()
c.execute('SELECT Recipe FROM Recipes WHERE Dish_Type = :selection ORDER BY Recipe DESC',
{'selection': selection})
results = c.fetchall()
for recipe, in result:
    s = '{0:>8}'.format(recipe)
    output.insert(END, s)
This does not work either... even after a number of modifications to line 7 I could not get it to work correctly. I could get it to insert, but the data would insert centered and not to the Left.


RE: how do i add a margin to a Listbox - Larz60+ - Apr-02-2019

Quote:I'm not sure how much of a learning curve it is compared to tkinter but I will certainly give it a try.
I learned tkinter first, then wxpython. There was about a 5 to 1 ratio, wxpython in 1/5 of the time. The greatest help is the demo program. You need to be sure and download it. each example has 3 parts, on three tabs and you can switch back and forth just by clicking on a tab. One is a functional example that graphically shows most if not all of it's capabilities, one explaining the widget, and the third, the actual code that created the demo. It's one of the most outstanding demo's I've ever encountered.

Get a visual quick look at many of the widgets in the gallery: https://docs.wxpython.org/gallery.html. It has a clean look.

Geometry is a snap (sizers take a bit of time to grasp, but work as advertised). Docking and resizing is a snap!


RE: how do i add a margin to a Listbox - wuf - Apr-02-2019

Hi DT2000

My sample is also working with pack only:
import tkinter as tk

class Application(object):
    INTERIOR_BORDER = 10
    
    def __init__(self, mainm_win):
        self.main_win = main_win
        
        self.build()
    
    def build(self):
        main_frame = tk.Frame(self.main_win, relief='sunken', bd=1, bg='white')
        main_frame.pack(fill='both', expand=True, padx=5, pady=5)
        
        list_item_values = ["one", "two", "three", "four", "five"]*5
        
        listbox = tk.Listbox(main_frame, width=42, height=20, bd=0,
            highlightthickness=0)
        listbox.pack(side='left', fill='both',expand=True,
            padx=self.INTERIOR_BORDER, pady=self.INTERIOR_BORDER)
        listbox.insert('end', *list_item_values)

        yscroll1 = tk.Scrollbar(main_frame, command=listbox.yview,
            orient=tk.VERTICAL)
        yscroll1.pack(side='left', fill='y')
        
        listbox.configure(yscrollcommand=yscroll1.set)
    
        listbox.bind('<ButtonRelease-1>', self.get_recipe)
        
    def get_recipe(self, event=None):
        print("Get Recipe")
        
if __name__ == "__main__":
    main_win = tk.Tk()
    main_win.title("Listbox with internal border")
    app = Application(main_win)
    main_win.mainloop()
wuf :-)