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. _
"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.
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.
Related
So I tried to make this labelframe wider by using the basic width and width option.
Here's my given minimal code.
from tkinter import *
from tkinter import ttk
app = Tk()
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.grid()
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.grid()
ttk.Label(settings, text="Length limit (in seconds)").grid()
ttk.Spinbox(settings, from_=60, to=600, width=4).grid()
app.mainloop()
minimalized preview:
used in application:
i want to get this labelframe little bit bigger and make the inside centered, But i had no knowledge to do so, Any help will apreciated!
It seems like you just want to have a main_frame in the app. For simplicity I've used .pack with the options fill and expand with the constants tkinter.BOTH to stretch the widget in both (x,y) direction and True to consume extra space. (This is one of the reasons why wildcard imports are discouraged, you can be unaware of overwriting something, use import tkinter as tk instead). Same happens with the LabelFrame, you may could delete one of the containers, but that is up to you.
In LabelFrame I have configured the grid and gave the instruction that the column 0 should get the extra space with the priority/weight 1.
In addition, I gave your Spinbox a little bit more width, changed the size of the window and separated the constructor from the geometrymethod.
To get in touch with the geometry management in tkinter, you could play around with the instructions (e.g. comment some out) and see what happens.
from tkinter import *
from tkinter import ttk
app = Tk()
app.geometry('500x500')
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.pack(fill=BOTH,expand=True)
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.pack(fill=BOTH,expand=True)
settings.columnconfigure(0,weight=1)
my_label = ttk.Label(settings, text="Length limit (in seconds)")
my_label.grid()
my_spinbox = ttk.Spinbox(settings, from_=60, to=600, width=20)
my_spinbox.grid()
app.mainloop()
I'm working with Tkinter in Python 2.7 on Windows 7, and found the need to create a popup box with a tree-style list of checkboxes. I could not find this in Tkinter, or ttk. I did, however, find it in Tix in the CheckList widget. I got a working standalone example using Tix, but I cannot figure out how to add my Tix.CheckList to my ttk.Frame that controls my main program.
Surely I am not forced to use Tix framework from the ground up?
import Tix
import pandas as pd
import Tkinter as tk
class TreeCheckList(object):
def __init__(self, root):
self.root = root
self.cl = Tix.CheckList(self.root)
self.cl.pack(fill=Tix.BOTH, expand=Tix.YES)
self.cl.hlist.config(bg='white', bd=0, selectmode='none', selectbackground='white', selectforeground='black', drawbranch=True, pady=5)
self.cl.hlist.add('ALL', text='All Messages')
self.cl.hlist.add('ALL.First', text='First')
self.cl.setstatus('ALL.First', "off")
self.cl.hlist.add('ALL.Second', text='Second')
self.cl.setstatus('ALL.Second', "off")
self.cl.autosetmode()
def main():
root = Tix.Tk()
top = Tix.Toplevel(root)
checklist = TreeCheckList(top)
root.update()
top.tkraise()
root.mainloop()
if __name__ == '__main__':
main()
The above code works in a standalone program using all Tix widgets. However, when I try to implement this into my larger program, I receive a TclError: invalid command name "tixCheckList"
To simulate this in the standalone, I changed the lines:
root = Tix.Tk()
top = Tix.Toplevel(root)
to
root = tk.Tk()
top = tk.Toplevel(root)
I was hoping I could just implement a Tix.Toplevel, placing it on a tk.Tk() root, but same issue.
Am I only allowed to use Tix frames when using a Tix widget, or am I misunderstanding something? If anyone has good Tix documentation, I would LOVE whatever I can get. It seems good docs on it are few and far between. Or is this same functionality included in ttk and I've just overlooked it? It seems to be one of the only things left out.
I have just learned that apparently only root needs to be a Tix class. Since Tk, and therefore ttk, classes appear to be added to the Tix root just fine (since most of them extend the Tkinter classes anyway), this appears to be a "fix". So my problem may have been solved by changing just
root = tk.Tk()
to
root = Tix.Tk()
This did require that I pull Tix into a part of my program I wasn't wanting for encapsulation purposes, but I guess there's no other way.
Hi I am new to programming and I apologize if this is an obvious mistake. I am writing a gui on tkinter using python 3.5 on Mac OsX El Capitan. Here is the code so far:
from tkinter import *
from tkinter import ttk
class GUI(object):
def __init__(self, master):
master.title("Title")
master.resizable(False, False)
self.frame1 = ttk.Frame(master)
self.frame1.pack()
ttk.Label(text="Organism").grid(row=1, column=0)
self.organism_picker = ttk.Combobox(self.frame1, values=("Drosophila melanogaster",
"Danio rerio",
"Caenorhabditis elegans",
"Rattus norvegicus",
"Mus musculus",
"Homo sapiens"))
self.organism_picker.grid(row=2, column=0)
ttk.Label(text="Core gene symbol:").grid(row=3, column=0)
self.core = ttk.Entry(self.frame1)
self.core.grid(row=4, column=0)
root = Tk()
gui = GUI(root)
root.mainloop()
When I run this the program enters the main loop, but the gui window never appears and the launcher is not reponding.
I tried to reinstall python 3, installed ActiveTcl, tried using ActivePython instead. Nut none of it worked.
Thank you all in advance for your answers.
You should not use pack() and grid() as this will generate a geometry manager conflict as pointed out in this error:
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
try changing:
self.frame1.pack()
to:
self.frame1.grid()
In this case I would suggest using pack overall, since this is a fairly simple layout. please refer to this guide:
When to use the Pack Manager
Compared to the grid manager, the pack manager is somewhat limited,
but it’s much easier to use in a few, but quite common situations:
Put a widget inside a frame (or any other container widget), and have
it fill the entire frame Place a number of widgets on top of each
other Place a number of widgets side by side
AND FINALLY:
Note: Don’t mix grid and pack in the same master window. Tkinter will
happily spend the rest of your lifetime trying to negotiate a solution
that both managers are happy with. Instead of waiting, kill the
application, and take another look at your code. A common mistake is
to use the wrong parent for some of the widgets.
The only problem with your code is that you forgot to attach the two labels to the the main widget self.frame1.
To fix that, modify them as follow:
#Attach the 2 labels to self.frame1
ttk.Label(self.frame1,text="Organism").grid(row=1, column=0)
ttk.Label(self.frame1,text="Core gene symbol:").grid(row=3, column=0)
Demo
After doing that, you will get this:
With the default theme on Linux, I can't get the button to have a border. Here's my attempt:
from tkinter import *
from tkinter.ttk import *
tk = Tk()
style = Style()
style.configure('TButton.border', background='red')
button = Button(tk, text='Hello')
button.pack()
mainloop()
I have found theming very difficult because it is not clear what I can change. Sometimes changes seem to do nothing.
It depends on what theme you're using; not all themes associate all widgets with all the elements they might have, and some (e.g., the OSX native theme, aqua) really very tightly control what the look and feel of many elements are (since the theme engine in that case delegates to the native drawing code). It's entirely possible that the theme you're using just doesn't permit red borders at all.
Try switching to a different theme…
ttk.Style().theme_use('clam')
Simple proposal ... if you really want a border. Understand it should not be satisfactory.
import Tkinter as tk
root = tk.Tk()
def nothing(event=None):
print "click"
bgbutton= tk.Button(root, )
bgbutton.pack()
bgbutton.configure(relief=tk.GROOVE, borderwidth=5, background="#2244FF", activebackground="#FF0000", highlightcolor="#00FF00")
button= tk.Button(bgbutton, text="Glance at my border", command=nothing)
button.pack()
button.configure(relief=tk.GROOVE, borderwidth=2)
root.mainloop()
I am trying to learn Python and trying something GUI in Python and came across this Tkinter module. My code runs but the window does not appear when I run. My code is as follows:
from Tkinter import *
#to create a root window
root = Tk()
The program runs, gives no errors but the window does not show up.
Add this to your code root.mainloop(), Here's a tutorial.
In response to your comment
#Also note that `from <module> import *` is generally frowned upon
#since it can lead to namespace collisions. It's much better to only
#explicitly import the things you need.
from Tkinter import Tk, Label
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
As other answers have pointed out, you need to call mainloop on the root object.
I recommend an OO-style of programming, and I also recommend not doing a global import (ie: not 'from Tkinter import *').
Here's a template I usually start out with:
import Tkinter as tk
class ExampleView(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
l = tk.Label(self, text="your widgets go here...", anchor="c")
l.pack(side="top", fill="both", expand=True)
if __name__=='__main__':
root = tk.Tk()
view = ExampleView(root)
view.pack(side="top", fill="both", expand=True)
root.mainloop()
This makes it easy to keep your main logic at the start of the file, and keep the creation of the root and the calling of mainloop together, which I think makes the code a little bit easier to understand. It also makes reusing this code a little easier (ie: you could create a larger program where this is one of several windows that can be created)
Add root.mainloop() at the end.