Apr-05-2019, 08:29 AM
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:
N.B. Whishing you a lot of fun with Tkinter & Python.
Greetings wuf :-)
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 :-)