from tkinter import *
class MainWindow(Tk): # класс - Главного Окно ####################################################################
# (Дочернее-Окно)
CHILD = None
def __init__(self, width, height, title='MyWindow', resizable=(False, False), icon=None):
super().__init__()
# (Главное-Окно)
self.title(title)
self.geometry(f'{width}x{height}+250+200')
self.resizable(resizable[0], resizable[1])
if icon is not None:
self.iconbitmap(icon)
# (Клавиша)
self.btn = Button(self, text='Создать Дочернее Окно', command=self.btn_on_pressed)
def run(self): # запуск Главного-Окна
self.draw_widgets()
self.mainloop()
def draw_widgets(self): # вывод в Главное-Окно всех виджетов
self.btn.pack()
def create_child(self, width, height, title='Child', resizable=(False, False), icon=None): # создание Дочернего-Окна
if MainWindow.CHILD is not None: # удаление предыдущего экземпляра Дочернего-Окна
self.delete_child()
MainWindow.CHILD = None
MainWindow.CHILD = ChildWindow(self, width, height, title, resizable, icon) # создание нового Дочернего-Окна
@classmethod
def delete_child(cls): # уничтожение Дочернего Окна
cls.CHILD.child_root.destroy()
def btn_on_pressed(self):
self.create_child(300, 300, 'Child', (False, False))
class ChildWindow: # класс - Дочернее-Окно #######################################################################
def __init__(self, master, width, height, title='Child', resizable=(False, False), icon=None):
# (Главное Окно)
self.master = master
# (Дочернее-Окно)
self.child_root = Toplevel(master)
self.child_root.title(f'{title}')
self.child_root.geometry(f'{width}x{height}+800+200')
self.child_root.resizable(resizable[0], resizable[1])
if icon is not None:
self.child_root.iconbitmap(icon)
# обращение:
if __name__ == '__main__': # ДЛЯ ТЕСТИРОВАНИЯ (в этом же Модуле)
main_win = MainWindow(300, 200, 'Main Window') # создание Главного-Окна
main_win.run() # запуск Главного-Окна
- Здравствуйте все! Доброго всем времени суток! Мой вопрос понятен из самого заголовка.
Помогите пожалуйста. Заранее благодарен!