Python tkinter class multiple windows

Using tkinter, I’m trying to open one window from another window and doing so by creating the windows in a class.

This question talks about tkinter and class, but not multiple windows: Python Tkinter with classes

This question addresses multiple windows but it’s creating an instance of the class: Python tkinter multiple windows. I don’t want to create an instance of the class because tkdoc.com has the root = Tk() being passed into the class rather than creating an instance.

So, I have this example from https://www.pythontutorial.net/tkinter/tkinter-toplevel/ which does what I want but it creates a subclass of tk.TK rather than passing in root. I’m trying to adapt this example to pass in root because that’s what the official docs do.

Here’s the example of one window opening up another window that works, but it creates a subclass of tk.Tk:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import tkinter as tk
from tkinter import ttk
class Window(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Toplevel Window')
ttk.Button(self,
text='Close',
command=self.destroy).pack(expand=True)
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('300x200')
self.title('Main Window')
# place a button on the root window
ttk.Button(self,
text='Open a window',
command=self.open_window).pack(expand=True)
def open_window(self):
window = Window(self)
window.grab_set()
if __name__ == "__main__":
app = App()
app.mainloop()
</code>
<code>import tkinter as tk from tkinter import ttk class Window(tk.Toplevel): def __init__(self, parent): super().__init__(parent) self.geometry('300x100') self.title('Toplevel Window') ttk.Button(self, text='Close', command=self.destroy).pack(expand=True) class App(tk.Tk): def __init__(self): super().__init__() self.geometry('300x200') self.title('Main Window') # place a button on the root window ttk.Button(self, text='Open a window', command=self.open_window).pack(expand=True) def open_window(self): window = Window(self) window.grab_set() if __name__ == "__main__": app = App() app.mainloop() </code>
import tkinter as tk
from tkinter import ttk


class Window(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)

        self.geometry('300x100')
        self.title('Toplevel Window')

        ttk.Button(self,
                text='Close',
                command=self.destroy).pack(expand=True)


class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('300x200')
        self.title('Main Window')

        # place a button on the root window
        ttk.Button(self,
                text='Open a window',
                command=self.open_window).pack(expand=True)

    def open_window(self):
        window = Window(self)
        window.grab_set()


if __name__ == "__main__":
    app = App()
    app.mainloop()

Here’s my adaptation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from tkinter import *
from tkinter import ttk
class Window(Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Toplevel Window')
ttk.Button(self,
text='Close',
command=self.destroy).pack(expand=True)
class App():
def __init__(self, root):
super().__init__()
root.geometry('300x200')
root.title('Main Window')
# place a button on the root window
ttk.Button(root,
text='Open a window',
command=self.open_window).pack(expand=True)
def open_window(self):
window = Window(self)
window.grab_set()
if __name__ == "__main__":
root = Tk()
App(root)
root.mainloop()
</code>
<code>from tkinter import * from tkinter import ttk class Window(Toplevel): def __init__(self, parent): super().__init__(parent) self.geometry('300x100') self.title('Toplevel Window') ttk.Button(self, text='Close', command=self.destroy).pack(expand=True) class App(): def __init__(self, root): super().__init__() root.geometry('300x200') root.title('Main Window') # place a button on the root window ttk.Button(root, text='Open a window', command=self.open_window).pack(expand=True) def open_window(self): window = Window(self) window.grab_set() if __name__ == "__main__": root = Tk() App(root) root.mainloop() </code>
from tkinter import *
from tkinter import ttk


class Window(Toplevel):
    def __init__(self, parent):
        super().__init__(parent)

        self.geometry('300x100')
        self.title('Toplevel Window')

        ttk.Button(self,
                text='Close',
                command=self.destroy).pack(expand=True)


class App():
    def __init__(self, root):
        super().__init__()

        root.geometry('300x200')
        root.title('Main Window')

        # place a button on the root window
        ttk.Button(root,
                text='Open a window',
                command=self.open_window).pack(expand=True)

    def open_window(self):
        window = Window(self)
        window.grab_set()


if __name__ == "__main__":
    root = Tk()
    App(root)
    root.mainloop()

I’m getting an AttributeError that is bewildering me:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Traceback (most recent call last):
File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "c:UsersUserDocumentsPythonPython_Tutorial_netMultiple_Windows_pass-in-Class.py", line 30, in open_window
window = Window(self)
^^^^^^^^^^^^
File "c:UsersUserDocumentsPythonPython_Tutorial_netMultiple_Windows_pass-in-Class.py", line 7, in __init__
super().__init__(parent)
File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 2678, in __init__
BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 2623, in __init__
self._setup(master, cnf)
File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 2592, in _setup
self.tk = master.tk
^^^^^^^^^
AttributeError: 'App' object has no attribute 'tk'
</code>
<code>Traceback (most recent call last): File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 1948, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "c:UsersUserDocumentsPythonPython_Tutorial_netMultiple_Windows_pass-in-Class.py", line 30, in open_window window = Window(self) ^^^^^^^^^^^^ File "c:UsersUserDocumentsPythonPython_Tutorial_netMultiple_Windows_pass-in-Class.py", line 7, in __init__ super().__init__(parent) File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 2678, in __init__ BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 2623, in __init__ self._setup(master, cnf) File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 2592, in _setup self.tk = master.tk ^^^^^^^^^ AttributeError: 'App' object has no attribute 'tk' </code>
Traceback (most recent call last):
  File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 1948, in __call__        
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "c:UsersUserDocumentsPythonPython_Tutorial_netMultiple_Windows_pass-in-Class.py", line 30, in open_window
    window = Window(self)
             ^^^^^^^^^^^^
  File "c:UsersUserDocumentsPythonPython_Tutorial_netMultiple_Windows_pass-in-Class.py", line 7, in __init__
    super().__init__(parent)
  File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 2678, in __init__
    BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
  File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 2623, in __init__
    self._setup(master, cnf)
  File "C:UsersUserAppDataLocalProgramsPythonPython312Libtkinter__init__.py", line 2592, in _setup
    self.tk = master.tk
              ^^^^^^^^^
AttributeError: 'App' object has no attribute 'tk'

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật