Notebook widget in Tkinter

前端 未结 5 1657
孤独总比滥情好
孤独总比滥情好 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:21

    If anyone still looking, I have got this working as Tab in tkinter. Play around with the code to make it function the way you want (for example, you can add button to add a new tab):

    from tkinter import *
    
    class Tabs(Frame):
    
        """Tabs for testgen output"""
    
        def __init__(self, parent):
            super(Tabs, self).__init__()
            self.parent = parent
            self.columnconfigure(10, weight=1)
            self.rowconfigure(3, weight=1)
            self.curtab = None
            self.tabs = {}
            self.addTab()                
            self.pack(fill=BOTH, expand=1, padx=5, pady=5)
    
        def addTab(self):
            tabslen = len(self.tabs)
            if tabslen < 10:
                tab = {}
                btn = Button(self, text="Tab "+str(tabslen), command=lambda: self.raiseTab(tabslen))
                btn.grid(row=0, column=tabslen, sticky=W+E)
    
                textbox = Text(self.parent)
                textbox.grid(row=1, column=0, columnspan=10, rowspan=2, sticky=W+E+N+S, in_=self)
    
                # Y axis scroll bar
                scrollby = Scrollbar(self, command=textbox.yview)
                scrollby.grid(row=7, column=5, rowspan=2, columnspan=1, sticky=N+S+E)
                textbox['yscrollcommand'] = scrollby.set
    
                tab['id']=tabslen
                tab['btn']=btn
                tab['txtbx']=textbox
                self.tabs[tabslen] = tab
                self.raiseTab(tabslen)
    
        def raiseTab(self, tabid):
            print(tabid)
            print("curtab"+str(self.curtab))
            if self.curtab!= None and self.curtab != tabid and len(self.tabs)>1:
                    self.tabs[tabid]['txtbx'].lift(self)
                    self.tabs[self.curtab]['txtbx'].lower(self)
            self.curtab = tabid
    
    
    def main():
        root = Tk()
        root.geometry("600x450+300+300")
        t = Tabs(root)
        t.addTab()
        root.mainloop()
    
    if __name__ == '__main__':
        main()
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 21:33

    What problems did you have with pmw? It's old, yes, but it's pure python so it should work.

    Note that Tix doesn't work with py2exe, if that is an issue for you.

    0 讨论(0)
  • 2020-12-13 21:34

    While it may not help you at the moment, tk 8.5 comes with an extended set of widgets. This extended set is available with tk 8.4 by way of an extension known as "tile". Included in the extended set of widgets is a notebook widget. Unfortunately, at this time Tkinter by default uses a fairly old version of Tk that doesn't come with these widgets.

    There have been efforts to make tile available to Tkinter. Check out http://tkinter.unpythonic.net/wiki/TileWrapper. For another similar effort see http://pypi.python.org/pypi/pyttk. Also, for a taste of how these widgets look (in Ruby, Perl and Tcl) see http://www.tkdocs.com/.

    Tk 8.5 is a huge improvement over stock Tk. It introduces several new widgets, native widgets, and a theming engine. Hopefully it will be available by default in Tkinter some day soon. Too bad the Python world is lagging behind other languages.

    update: The latest versions of Python now include support for the themed widgets out of the box. _

    0 讨论(0)
  • 2020-12-13 21:41

    "Or is it simply the case that anyone who needs more powerful windowing components has to use wxPython?"
    Short answer: yes.

    Long answer: It may take some practice for your wxPython code to feel "clean," but it is nicer and much more powerful than Tkinter. You will also get better support, since more people use it these days.

    0 讨论(0)
提交回复
热议问题