Home > Enterprise >  Reuse Tkinter code, within different windows
Reuse Tkinter code, within different windows

Time:11-04

I have a GUI which contains buttons that when clicked, open a new window of which has the same Frames as the root. It also withdraws the root window from view. I used the "pen-and-paper" method in order to work out the positioning of my Frames.

I want to reuse the same Frame layout throughout the GUI as I plan on increasing the number of buttons, but don't want to repeat the same code. What would be the best method to organise the code in a more efficient manor?

Here is my script:

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.geometry('')
root.resizable(False, False)

# Functions
def exitApplication():
    MsgBox = messagebox.askquestion('Exit Application', 'Are you sure you want to exit?', icon='warning')
    if MsgBox == 'yes':
        root.destroy()

def hide(window):
    window.withdraw()

def show(window):
    window.deiconify()
    window.update()

def button1Window():
    hide(root)
    global icon
    btn1Win = tk.Toplevel(root)
    btn1Win.title('Button 1')
    btn1Win.geometry('')

    # Frames
    topFrm = tk.Frame(btn1Win)
    topFrm.pack(fill=tk.BOTH)

    middleFrm = tk.Frame(btn1Win, pady=10, highlightbackground="black", highlightthickness=1)
    middleFrm.pack(fill=tk.BOTH)

    bottomFrm = tk.Frame(btn1Win)
    bottomFrm.pack(fill=tk.BOTH)

    endFrm = tk.Frame(btn1Win)
    endFrm.pack(fill=tk.BOTH)

    # Labels
    btn1WinLbl = tk.Label(topFrm, text='Button 1 Window')
    btn1WinLbl.pack()

    firstLbl = tk.Label(middleFrm, text='This is the content for Button 1. '
                                        '\n More information will be added in the future.')
    firstLbl.grid(row=0, columnspan=3)

    versionLbl = tk.Label(endFrm, text='Version 0.1 2021', )
    versionLbl.config(font=('Times New Roman', 5))
    versionLbl.pack(side=tk.RIGHT)

    # Buttons
    tk.Button(btn1Win, text='Back to Home', command=lambda:[show(root), btn1Win.destroy()]).pack()
    btn1Win.protocol("WM_DELETE_WINDOW", exitApplication)

def button2Window():
    hide(root)
    global icon
    btn2Win = tk.Toplevel(root)
    btn2Win.title('Button 2')
    btn2Win.geometry('')

    # Frames
    topFrm = tk.Frame(btn2Win)
    topFrm.pack(fill=tk.BOTH)

    middleFrm = tk.Frame(btn2Win, pady=10, highlightbackground="black", highlightthickness=1)
    middleFrm.pack(fill=tk.BOTH)

    bottomFrm = tk.Frame(btn2Win)
    bottomFrm.pack(fill=tk.BOTH)

    endFrm = tk.Frame(btn2Win)
    endFrm.pack(fill=tk.BOTH)

    # Labels
    btn1WinLbl = tk.Label(topFrm, text='Button 2 Window')
    btn1WinLbl.pack()

    firstLbl = tk.Label(middleFrm, text='This is the content for Button 2. '
                                        '\n This is for Vegetables.'
                                        '\n More Vegetables will be added in the future.')
    firstLbl.grid(row=0, columnspan=3)

    versionLbl = tk.Label(endFrm, text='Version 0.1 2021', )
    versionLbl.config(font=('Times New Roman', 5))
    versionLbl.pack(side=tk.RIGHT)

    # Buttons
    tk.Button(btn2Win, text='Back to Home', command=lambda:[show(root), btn2Win.destroy()]).pack()
    btn2Win.protocol("WM_DELETE_WINDOW", exitApplication)

def button3Window():
    hide(root)
    global icon
    btn3Win = tk.Toplevel(root)
    btn3Win.title('Button 3')
    btn3Win.geometry('')

    # Frames

    topFrm = tk.Frame(btn3Win)
    topFrm.pack(fill=tk.BOTH)

    middleFrm = tk.Frame(btn3Win, pady=10, highlightbackground="black", highlightthickness=1)
    middleFrm.pack(fill=tk.BOTH)

    bottomFrm = tk.Frame(btn3Win)
    bottomFrm.pack(fill=tk.BOTH)

    endFrm = tk.Frame(btn3Win)
    endFrm.pack(fill=tk.BOTH)

    # Labels
    btn3WinLbl = tk.Label(topFrm, text='Button 3 Window')
    btn3WinLbl.pack()

    firstLbl = tk.Label(middleFrm, text='This is the content for Button 3. '
                                        '\n This is for Fruit'
                                        '\n More Fruit will be added in the future.')
    firstLbl.grid(row=0, columnspan=3)

    versionLbl = tk.Label(endFrm, text='Version 0.1 2021', )
    versionLbl.config(font=('Times New Roman', 5))
    versionLbl.pack(side=tk.RIGHT)

    # Buttons
    tk.Button(btn3Win, text='Back to Home', command=lambda:[show(root), btn3Win.destroy()]).pack()
    btn3Win.protocol("WM_DELETE_WINDOW", exitApplication)

# Create Frames first, and fill with widgets later
topFrm = tk.Frame(root)
topFrm.pack(fill=tk.BOTH)

middleFrm = tk.Frame(root, pady=10, highlightbackground="black", highlightthickness=1)
middleFrm.pack(fill=tk.BOTH)

bottomFrm = tk.Frame(root)
bottomFrm.pack(fill=tk.BOTH)

endFrm = tk.Frame(root)
endFrm.pack(fill=tk.BOTH)

# Label widgets
titleLbl = tk.Label(topFrm, text='Homepage')
titleLbl.pack()

firstLbl = tk.Label(middleFrm, text='Choose one of the following options.\n A new window will open.')
firstLbl.grid(row=0, columnspan=4)

versionLbl = tk.Label(endFrm, text='Version 0.1 2021',)
versionLbl.config(font=('Times New Roman', 5))
versionLbl.pack(side=tk.RIGHT)

# Button widgets
Btn1 = tk.Button(middleFrm, text="Button 1", width=10, height=2,
                       command=button1Window).grid(row=1, column=0, padx=3, pady=3)

Btn2 = tk.Button(middleFrm, text="Button 2", width=10, height=2,
                       command=button2Window).grid(row=1, column=1, padx=3, pady=3)

Btn3 = tk.Button(middleFrm, text="Button 3", width=10, height=2,
                       command=button3Window).grid(row=1, column=3, padx=3, pady=3)

exitBtn = tk.Button(bottomFrm, text="Exit", width=10, height=1, command=exitApplication,
                         ).pack(padx=5, pady=5)

# Call exitApplication function if Red X clicked
root.protocol("WM_DELETE_WINDOW", exitApplication)
root.mainloop()

CodePudding user response:

Making a new function for the frame can help, so for example, add the common stuff, and pass texts in the func (This function can also be more simple) -

def create_frame_layout(
    n,win_lbl,content): # Added some variables to be passed
    hide(root)

    container = tk.Toplevel(root)
    container.title(f'Button {n}')
    container.geometry('')

    topFrm = tk.Frame(container)
    topFrm.pack(fill=tk.BOTH)

    middleFrm = tk.Frame(container, pady=10, highlightbackground="black", highlightthickness=1)
    middleFrm.pack(fill=tk.BOTH)

    bottomFrm = tk.Frame(container)
    bottomFrm.pack(fill=tk.BOTH)

    endFrm = tk.Frame(container)
    endFrm.pack(fill=tk.BOTH)

    tk.Button(container, text='Back to Home', command=lambda:[show(root), container.destroy()]).pack()
    container.protocol("WM_DELETE_WINDOW", exitApplication)

    btn1WinLbl = tk.Label(topFrm, text=win_lbl)
    btn1WinLbl.pack()

    firstLbl = tk.Label(middleFrm, text=content)
    firstLbl.grid(row=0, columnspan=3)

    versionLbl = tk.Label(endFrm, text='Version 0.1 2021', )
    versionLbl.config(font=('Times New Roman', 5))
    versionLbl.pack(side=tk.RIGHT)

Then simply call the function with the required text -

def button1Window():
    global icon
    
    num = 1
    win_lbl = 'Button 1 Window' # Just provide the text you want
    btn_content = 'This is the content for Button 1.\n More information will be added in the future.'

    create_frame_layout(num,win_lbl,btn_content)

def button2Window():
    global icon
    
    num = 2 
    win_lbl = 'Button 2 Window' # Just provide the text you want
    btn_content = 'This is the content for Button 2.\n This is for Vegetables.\n More Vegetables will be added in the future.'

    create_frame_layout(num,win_lbl,btn_content) # etc...
  • Related