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 many features; in particular it doesn't have tabs (as in, the tabs at the top of a Firefox window).
A little Googling on the subject offers a few suggestions. There's a cookbook entry with a class allowing you to use tabs, but it's very primitive. There's also Python megawidgets on SourceForge, although this seems very old and gave me errors during installation.
Does anyone have experience making tabbed GUIs in Tkinter? What did you use? Or is it simply the case that anyone who needs more powerful windowing components has to use wxPython?
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
.
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()
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. _
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With