简体   繁体   中英

Python Tkinter - How add a notebook class into a tk.toplevel?

I need to have a notebook class because I want to create frames instances and then add to it. The problem is that ttk.Notebook needs the root window and I don't know how to give him the top-level root master.

That's the code:

class Notebook:
    def __init__(self,parent):
        super().__init__(parent)
        self.notebook=ttk.Notebook(parent) #if i give this, the program will add the notebook to the main window
        self.notebook.grid(row=1,column=0)

    def add_tab(self,title):
        frame=ttk.Frame(self.notebook,width=1280, height=280)
        frame.pack(fill='both',expand=True)
        self.notebook.add(frame,text=title)
        tx=tk.Text(frame)
        tx.pack()

class MainChat(tk.Toplevel): 
    def __init__(self, parent):
        super().__init__(parent)
        self.master=parent
        self.title("Test")
        self.iconbitmap("icon.ico")
        self.resizable(False,False)
        self.columnconfigure(0,weight=1)
        self.createwidg()

    def createwidg(self):
        self.titlelb=ttk.Label(self,text="Test",font=("Helvetica",16))
        self.nb1=Notebook(self.master)

        self.titlelb.grid(row=0,column=0,pady=(10,0))
        
        self.nb1.add_tab('Test')

Main problem: you need

 Notebook(self) 

without .master


Second problem: your class Notebook is not real tkinter widget so it doesn't need super().__init__(parent) but standard super().__init__() . And even you can skip this line.


Minimal working code:

import tkinter as tk
from tkinter import ttk


class Notebook:
    
    def __init__(self, parent):
        #super().__init__(parent)  # no need it
        self.notebook = ttk.Notebook(parent)
        self.notebook.grid(row=1, column=0)

    def add_tab(self, title):
        frame = ttk.Frame(self.notebook, width=1280, height=280)
        frame.pack(fill='both', expand=True)

        self.notebook.add(frame, text=title)

        tx = tk.Text(frame)
        tx.pack()


class MainChat(tk.Toplevel):
    
    def __init__(self, parent):
        super().__init__(parent)
        self.master = parent
        self.title("Test")
        #self.iconbitmap("icon.ico")
        self.resizable(False, False)
        self.columnconfigure(0, weight=1)
        self.createwidg()

    def createwidg(self):
        self.titlelb = ttk.Label(self, text="Test", font=("Helvetica", 16))
        self.titlelb.grid(row=0, column=0, pady=(10, 0))
        
        self.nb1 = Notebook(self)  # without `.master`
        self.nb1.add_tab('Test')
        

def run():
    MainChat(root)


if __name__ == '__main__':        
    root = tk.Tk()
    b = tk.Button(root, text="Chat", command=run)
    b.grid(row=0, column=0)
    root.mainloop()

BTW:

PEP 8 -- StyleGuide for Python Code

ie. spaces around = , space after ,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM