简体   繁体   English

Python Tkinter - 如何将笔记本类添加到 tk.toplevel?

[英]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.问题是ttk.Notebook需要root窗口,不知道怎么给他顶级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没有.master


Second problem: your class Notebook is not real tkinter widget so it doesn't need super().__init__(parent) but standard super().__init__() .第二个问题:您的class Notebook不是真正的 tkinter 小部件,因此它不需要super().__init__(parent)而是标准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 PEP 8 -- Python 代码风格指南

ie. IE。 spaces around = , space after , =周围的空格,后面的空格,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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