#python #user-interface #tkinter
Вопрос:
У меня есть графический интерфейс, который содержит кнопки, которые при нажатии открывают новое окно, в котором есть те же рамки, что и в корне. Он также удаляет корневое окно из поля зрения. Я использовал метод «ручка и бумага», чтобы определить расположение своих кадров.
Я хочу повторно использовать один и тот же макет фрейма во всем графическом интерфейсе, так как планирую увеличить количество кнопок, но не хочу повторять один и тот же код. Каков был бы наилучший метод организации кода в более эффективной среде?
Вот мой сценарий:
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()
Ответ №1:
Создание новой функции для фрейма может помочь, например, добавить общие элементы и передать тексты в функции (эта функция также может быть более простой). —
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)
Затем просто вызовите функцию с требуемым текстом —
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...
Комментарии:
1. Примечание: то, что я сделал, было примером, вы могли бы сделать его более простым
btn1Win = tk.Toplevel(root)
, тоже сделав общим, но это сложно