Python Forum
[Tkinter] how do i add a margin to a Listbox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] how do i add a margin to a Listbox
#19
Hi DT2000

Thanks for presenting your complete script. I have replaced the 'place' instruction by 'pack' ones. Don't use 'place' for ordinary GUI applications. Try to enbed widget-groups in Frames. Also try to use classes for GUI-Applications.
Following i placed two scripts. The first one is not engaging the 'win32print'- and 'sqlite3'-module. The second includes these missing modules.
First script:
import os
#import tkinter
#import sqlite3
#import win32print
#from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

# Testdata for the listbox
TEST_DATA = list_item_values = ["one", "two", "three", "four", "five"]*5
# X&Y-Border for the listbox
INTERIOR_X_BORDER = 10
INTERIOR_Y_BORDER = 5

#===================================== MAIN ====================================
def main():
##============================= CONNECT TO DATABASE =============================
    #conn = sqlite3.connect('Recipe.db')
    #c = conn.cursor()
##========================= CREATE DATABASE WITH TABLES =========================
    #c.execute('''CREATE TABLE IF NOT EXISTS Recipes(
    #Dish_Type CHAR(50),
    #Recipe CHAR(50),
    #Cook_Time CHAR(15),
    #Serves CHAR(8),
    #Ingredients CHAR(10000) ,
    #Instructions CHAR(10000));''')
    #conn.commit()
 
    #c.execute('''CREATE TABLE IF NOT EXISTS Version(
    #Welcome Decimal(10));''')
    #conn.commit()
 
    #c.execute('''CREATE TABLE IF NOT EXISTS Welcome(
    #Welcome TEXT(2500));''')
    #conn.commit()
#================================== WINDOW 2 ===================================
    window_2 = tk.Tk()
    window_2.title('Recipes')
    w = 1024
    h = 612
    ws = window_2.winfo_screenwidth()
    hs = window_2.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    window_2.geometry('%dx%d+%d+%d' % (w, h, x, y)) 
#================================ SELECT QUERY =================================
    def select():
        try:
            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 results:
                output.insert(0,recipe)
        except:
            messagebox.showerror('Recipes', 'Failed to load category')
#================================ OPTION MENU ==================================
    var = tk.StringVar(window_2)
    var.set('Category')
    choices = [
        'Appetizers',
        'Beef',
        'Beverages',
        'Bakery',
        'Desserts',
        'Eggs',
        'Lamb',
        'Pasta',
        'Pork',
        'Potato',
        'Poultry',
        'Rice',
        'Salads',
        'Sandwiches',
        'Sauces/Dips',
        'Sea Food',
        'Slow Cooker',
        'Soups/Chili',
        'Stews',
        'Sushi',
        'Vegetables',
        'Vegetarian',
        'Wild Game']
        
    main_frame = tk.Frame(window_2)
    main_frame.pack(expand=True)
    
    # Container for option-button & other buttons
    option_frame = tk.Frame(main_frame)
    option_frame.pack(fill=tk.X, pady=(0, 20))
    
    # Container for listbox-frame & y-scrollbar
    listbox_container_frame = tk.Frame(main_frame)
    listbox_container_frame.pack(fill=tk.BOTH)

    # Container for the listbox widget only
    listbox_frame = tk.Frame(listbox_container_frame, bg='#FFD599')
    listbox_frame.pack(side=tk.LEFT, fill=tk.BOTH)
            
    option = tk.OptionMenu(option_frame, var, *choices)
    option.config(font=('Times 9 bold'), bg = '#F9F8D6', fg = '#9A0615')
    #option.place(x=210, y=136)
    option.pack(side=tk.LEFT)
    option['menu'].config(bg = '#F9F8D6', fg = '#9A0615')
    
    button = tk.Button(option_frame, text='Load Category',
        font=('Times 9 bold'), bg = '#F9F8D6', fg = '#9A0615', command=select)
    #button.place(x=362, y=139)
    button.pack(side=tk.LEFT)
#=================================== WINDGETS ==================================
    output = tk.Listbox(listbox_frame, font=('Times 10'), height=20,
        width=42, bd=0, bg='#FFD599', fg='#9A0615', selectmode=tk.SINGLE,
        highlightthickness=0)
    #output.place(x=210, y=195)
    output.pack(fill=tk.BOTH, padx=INTERIOR_X_BORDER, pady=INTERIOR_Y_BORDER)
    
    yscroll1 = tk.Scrollbar(listbox_container_frame, command=output.yview,
        orient=tk.VERTICAL)
    #yscroll1.place(x=463, y=196)
    yscroll1.pack(side=tk.LEFT, fill=tk.Y)
    
    output.configure(yscrollcommand=yscroll1.set)
    
    # For testfill of the listbox only
    # Please insert here your own app specific list data !!!
    output.insert('end', *list_item_values)
    
##    output.bind('<ButtonRelease-1>', getRecipe)
    window_2.mainloop()
    
    
if __name__ == '__main__':
    main()
Second script:
import os
#import tkinter
import sqlite3
import win32print
#from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

# Testdata for the listbox
TEST_DATA = list_item_values = ["one", "two", "three", "four", "five"]*5
# X&Y-Border for the listbox
INTERIOR_X_BORDER = 10
INTERIOR_Y_BORDER = 5

#===================================== MAIN ====================================
def main():
#============================= CONNECT TO DATABASE =============================
    conn = sqlite3.connect('Recipe.db')
    c = conn.cursor()
#========================= CREATE DATABASE WITH TABLES =========================
    c.execute('''CREATE TABLE IF NOT EXISTS Recipes(
    Dish_Type CHAR(50),
    Recipe CHAR(50),
    Cook_Time CHAR(15),
    Serves CHAR(8),
    Ingredients CHAR(10000) ,
    Instructions CHAR(10000));''')
    conn.commit()
 
    c.execute('''CREATE TABLE IF NOT EXISTS Version(
    Welcome Decimal(10));''')
    conn.commit()
 
    c.execute('''CREATE TABLE IF NOT EXISTS Welcome(
    Welcome TEXT(2500));''')
    conn.commit()
#================================== WINDOW 2 ===================================
    window_2 = tk.Tk()
    window_2.title('Recipes')
    w = 1024
    h = 612
    ws = window_2.winfo_screenwidth()
    hs = window_2.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    window_2.geometry('%dx%d+%d+%d' % (w, h, x, y)) 
#================================ SELECT QUERY =================================
    def select():
        try:
            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 results:
                output.insert(0,recipe)
        except:
            messagebox.showerror('Recipes', 'Failed to load category')
#================================ OPTION MENU ==================================
    var = tk.StringVar(window_2)
    var.set('Category')
    choices = [
        'Appetizers',
        'Beef',
        'Beverages',
        'Bakery',
        'Desserts',
        'Eggs',
        'Lamb',
        'Pasta',
        'Pork',
        'Potato',
        'Poultry',
        'Rice',
        'Salads',
        'Sandwiches',
        'Sauces/Dips',
        'Sea Food',
        'Slow Cooker',
        'Soups/Chili',
        'Stews',
        'Sushi',
        'Vegetables',
        'Vegetarian',
        'Wild Game']
        
    main_frame = tk.Frame(window_2)
    main_frame.pack(expand=True)
    
    # Container for option-button & other buttons
    option_frame = tk.Frame(main_frame)
    option_frame.pack(fill=tk.X, pady=(0, 20))
    
    # Container for listbox-frame & y-scrollbar
    listbox_container_frame = tk.Frame(main_frame)
    listbox_container_frame.pack(fill=tk.BOTH)
    
    # Container for the listbox widget only (used to get y-& x-pad for the
    # listbox interior
    listbox_frame = tk.Frame(listbox_container_frame, bg='#FFD599')
    listbox_frame.pack(side=tk.LEFT, fill=tk.BOTH)
    
    #listbox_container_frame = tk.Frame(listbox_frame, bg='#FFD599')
    #listbox_container_frame.pack(side=tk.LEFT, fill=tk.BOTH)
        
    option = tk.OptionMenu(option_frame, var, *choices)
    option.config(font=('Times 9 bold'), bg = '#F9F8D6', fg = '#9A0615')
    #option.place(x=210, y=136)
    option.pack(side=tk.LEFT)
    option['menu'].config(bg = '#F9F8D6', fg = '#9A0615')
    
    button = tk.Button(option_frame, text='Load Category',
        font=('Times 9 bold'), bg = '#F9F8D6', fg = '#9A0615', command=select)
    #button.place(x=362, y=139)
    button.pack(side=tk.LEFT)
#=================================== WINDGETS ==================================
    output = tk.Listbox(listbox_frame, font=('Times 10'), height=20,
        width=42, bd=0, bg='#FFD599', fg='#9A0615', selectmode=tk.SINGLE,
        highlightthickness=0)
    #output.place(x=210, y=195)
    output.pack(fill=tk.BOTH, padx=INTERIOR_X_BORDER, pady=INTERIOR_Y_BORDER)
    
    yscroll1 = tk.Scrollbar(listbox_container_frame, command=output.yview,
        orient=tk.VERTICAL)
    #yscroll1.place(x=463, y=196)
    yscroll1.pack(side=tk.LEFT, fill=tk.Y)
    
    output.configure(yscrollcommand=yscroll1.set)
    
    # For testfill of the listbox only
    # Please insert here your own app specific list data !!!
    output.insert('end', *TEST_DATA)
    
##    output.bind('<ButtonRelease-1>', getRecipe)
    window_2.mainloop()
    
    
if __name__ == '__main__':
    main()
Please study the scripts and try them out.
N.B. Whishing you a lot of fun with Tkinter & Python.

Greetings wuf :-)
Reply


Messages In This Thread
how do i add a margin to a Listbox - by DT2000 - Mar-31-2019, 06:04 PM
RE: how do i add a margin to a Listbox - by Larz60+ - Mar-31-2019, 08:14 PM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-01-2019, 07:14 AM
RE: how do i add a margin to a Listbox - by Larz60+ - Apr-01-2019, 08:44 AM
RE: how do i add a margin to a Listbox - by wuf - Apr-01-2019, 10:01 AM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-01-2019, 08:26 PM
RE: how do i add a margin to a Listbox - by Larz60+ - Apr-01-2019, 09:44 PM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-01-2019, 11:06 PM
RE: how do i add a margin to a Listbox - by Larz60+ - Apr-02-2019, 02:13 AM
RE: how do i add a margin to a Listbox - by wuf - Apr-02-2019, 11:46 AM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-02-2019, 07:30 PM
RE: how do i add a margin to a Listbox - by wuf - Apr-02-2019, 08:45 PM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-03-2019, 12:08 AM
RE: how do i add a margin to a Listbox - by Larz60+ - Apr-03-2019, 01:00 AM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-03-2019, 01:27 AM
RE: how do i add a margin to a Listbox - by wuf - Apr-03-2019, 05:53 AM
RE: how do i add a margin to a Listbox - by Larz60+ - Apr-03-2019, 02:35 PM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-04-2019, 11:56 PM
RE: how do i add a margin to a Listbox - by wuf - Apr-05-2019, 08:29 AM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-05-2019, 10:18 PM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-08-2019, 01:09 AM
RE: how do i add a margin to a Listbox - by wuf - Apr-08-2019, 06:51 AM
RE: how do i add a margin to a Listbox - by Larz60+ - Apr-08-2019, 08:05 AM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-09-2019, 05:18 AM
RE: how do i add a margin to a Listbox - by wuf - Apr-09-2019, 09:54 AM
RE: how do i add a margin to a Listbox - by DT2000 - Apr-09-2019, 08:25 PM
RE: how do i add a margin to a Listbox - by Larz60+ - Apr-09-2019, 08:49 PM
RE: how do i add a margin to a Listbox - by DT2000 - May-14-2019, 04:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Text Button - How Do I Reduce The Margin? vman44 6 11,306 Apr-27-2020, 10:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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