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
.
Best Solution
I will talk only about WxPython because it's the only toolkit I have experience with. TkInter is nice to write small programs (then it doesn't require a GUI Designer), but is not really appropriate for large application development.
wxFormBuilder is really good: it generates
.XRC
files you need to load in your program, and it can generate.py
files by subclassing them when you use.DialogBlocks and wxDesigner are two commercial software which can generate Python code directly. I didn't tested these much because of their price.
After trying all these, I realized they had all flaws and that nothing is better than just writing the GUI in an editor. The problem is the extended learning curve. But then you will be much more faster and your code will be much more flexible than when using a GUI designer.
Have a look at this list of major applications written with wxPython. You will probably see that none of these use a GUI Designer, there must be a reason for this.
You then understand gs is right when saying that either you switch to PyQt or you write your application by hand. I had a look at Qt Designer in the past and thought this was what I needed. Unfortunately PyQt has some license restrictions.