Apr-25-2020, 04:52 PM
As some of you know I've been working on improving my understanding of classes and methods on my project code. I've hit a roadblock that I'm not sure how to handle. I'm posting the whole script to show my setup. The roadblock is that I need to get the variable letter from the class Child2 to the database query in the class of Child3. The current script I have function call printing out the letter variable for testing. Is calling an outside function going to be the route I need to take? Or is there a way for me to call the class and get the variable to its destination? Any help would be much appreciated.
Thanks
Thanks
#! /usr/bin/env python3.8
'''cookbook'''
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import string
import webbrowser
from functools import partial
from PIL import Image, ImageTk
from modules import database as db, time_converter as tc
def link():
'''Open Webbrowser'''
webbrowser.open_new('http://recipes.phpshelf.net')
def get_letter(letter):
'''none'''
print(letter)
class RootWindow:
'''RootWindow'''
def __init__(self, master):
self.master = master
self.master.grid_columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)
self.logo_frame()
self.letter_frame()
self.container_frame()
self.title_frame()
self.recipe_frame()
self.footer_frame()
Child(self.logoframe, self.footerframe)
Child2(self.letterframe)
Child3(self.titleframe)
def logo_frame(self):
'''logo frame'''
self.logoframe = ttk.Frame(self.master, border=5, relief='ridge')
self.logoframe.grid(column=0, row=0, sticky='new')
self.logoframe.grid_columnconfigure(0, weight=3)
def letter_frame(self):
'''letterframe'''
self.letterframe = ttk.Frame(self.master, border=5, relief='ridge')
self.letterframe.grid(column=0, row=1, sticky='new')
for i in range(26):
self.letterframe.grid_columnconfigure(i, weight=3)
def container_frame(self):
'''Container frame will hold two frames. Title frame and recipe frame'''
self.containerframe = ttk.Frame(self.master)
self.containerframe.grid(column=0, row=2, sticky='new')
self.containerframe.grid_columnconfigure(0, weight=3)
def title_frame(self):
'''Title Frame'''
self.titleframe = ttk.Frame(self.containerframe, border=5, relief='ridge')
self.titleframe.grid(column=0, row=0, sticky='nw')
self.titleframe.grid_columnconfigure(0, weight=3)
def recipe_frame(self):
'''Recipe Frame'''
self.recipeframe = ttk.Frame(self.containerframe, border=5, relief='ridge')
self.recipeframe.grid(column=1, row=1)
self.recipeframe.grid_columnconfigure(0, weight=3)
def footer_frame(self):
'''footer'''
self.footerframe = ttk.Frame(self.master, border=5, relief='ridge')
self.footerframe.grid(column=0, row=3, sticky='new')
self.footerframe.grid_columnconfigure(0, weight=3)
class Child:
'''Child'''
def __init__(self, logoframe, footerframe):
self.my_logo(logoframe)
self.my_footer(footerframe)
def my_logo(self, logoframe):
'''logo'''
load = Image.open('/home/johnny/Desktop/CookBook/images/cookbook_logo.png')
render = ImageTk.PhotoImage(load)
self.logo = ttk.Label(logoframe, image=render)
self.logo.image = render
self.logo.grid(column=0, row=0)
def my_footer(self, footerframe):
'''footer'''
style = ttk.Style()
style.map('Footer.TLabel', \
foreground=[('pressed', 'firebrick'), ('active', 'red')], \
background=[('pressed', '!disabled', 'gray86'), ('active', 'gray86')] \
)
style.configure('Footer.TLabel', foreground='blue', \
font=('Times', 12, 'normal', 'underline'))
self.footer = ttk.Button(footerframe, text='My Footer', \
style='Footer.TLabel', cursor='hand2', command=link)
self.footer.grid(column=0, row=0, ipady=3)
class Child2:
'''Child2'''
#pylint: disable=R0903
def __init__(self, letterframe):
self.lettermenu(letterframe)
def lettermenu(self, letterframe):
'''lettermenu'''
letters = string.ascii_uppercase
i = 0
style = ttk.Style()
style.configure('Btn.TButton', width=4)
for letter in letters:
self.button = ttk.Button(letterframe, text=letter, style='Btn.TButton', \
command=partial(get_letter, letter))
self.button.grid(column=i, row=0, sticky='new')
i += 1
class Child3:
#pylint: disable=R0903
'''Child3 Class'''
def __init__(self, titleframe):
self.title_menu(titleframe)
def title_menu(self, titleframe):
'''title menu'''
data = db.Database().title_query(letter='a')
canvas = tk.Canvas(titleframe)
scrollbar = ttk.Scrollbar(titleframe, orient='vertical', command=canvas.yview)
scrollable_frame = ttk.Frame(canvas)
scrollable_frame.bind(
'<Configure>',
lambda e: canvas.configure(scrollregion=canvas.bbox('all'))
)
canvas.create_window((0, 0), window=scrollable_frame, anchor='nw')
canvas.configure(yscrollcommand=scrollbar.set)
letter = 'a'
if data:
self.old_letter = letter
if not data:
msg = messagebox.showerror(title='No listing', \
message='Sorry, there have not been any recipes added starting with ' + letter)
if msg == 'ok':
self.title_menu(self.old_letter)
style = ttk.Style()
style.map('L.TLabel', \
background=[('pressed', '!disabled', 'gray86'), ('active', '#ffffee')], \
foreground=[('pressed', 'red'), ('active', 'red')])
style.configure('L.TLabel', relief='flat', padding=2, foreground='blue')
for recipe_id, title in data:
title = ttk.Button(scrollable_frame, text=title.title(), \
style='L.TLabel', cursor='hand2')
title.grid(column=0, row=recipe_id, sticky='nw')
scrollbar.grid(column=0, row=0, sticky='nsw')
canvas.grid(column=1, row=0, sticky='nsw')
def main():
'''main'''
root = tk.Tk()
root.title('Johnny\'s CookBook')
root.resizable(width=False, height=False)
RootWindow(root)
root.mainloop()
if __name__ == '__main__':
main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
