Notebook widget in Tkinter

前端 未结 5 1666
孤独总比滥情好
孤独总比滥情好 2020-12-13 20:56

Having played around a little with both Tkinter and wxPython, I like Tkinter much better in terms of how clean my source code looks. However, it doesn\'t seem to have as ma

5条回答
  •  不知归路
    2020-12-13 21:27

    On recent Python (> 2.7) versions, you can use the ttk module, which provides access to the Tk themed widget set, which has been introduced in Tk 8.5.

    Here's how you import ttk in Python 2:

    import ttk
    
    help(ttk.Notebook)
    

    In Python 3, the ttk module comes with the standard distributions as a submodule of tkinter.

    Here's a simple working example based on an example from the TkDocs website:

    from tkinter import ttk
    import tkinter as tk
    from tkinter.scrolledtext import ScrolledText
    
    
    def demo():
        root = tk.Tk()
        root.title("ttk.Notebook")
    
        nb = ttk.Notebook(root)
    
        # adding Frames as pages for the ttk.Notebook 
        # first page, which would get widgets gridded into it
        page1 = ttk.Frame(nb)
    
        # second page
        page2 = ttk.Frame(nb)
        text = ScrolledText(page2)
        text.pack(expand=1, fill="both")
    
        nb.add(page1, text='One')
        nb.add(page2, text='Two')
    
        nb.pack(expand=1, fill="both")
    
        root.mainloop()
    
    if __name__ == "__main__":
        demo()
    

    Another alternative is to use the NoteBook widget from the tkinter.tix library. To use tkinter.tix, you must have the Tix widgets installed, usually alongside your installation of the Tk widgets. To test your installation, try the following:

    from tkinter import tix
    root = tix.Tk()
    root.tk.eval('package require Tix')
    

    For more info, check out this webpage on the PSF website.

    Note that tix is pretty old and not well-supported, so your best choice might be to go for ttk.Notebook.

提交回复
热议问题