56

Where can I find the most modern tutorial that teaches tkinter together with ttk?

Tkinter seems the only way to go in Python 3 (don't suggest Python 2), and ttk gave me hope for good-looking GUI.

nbro
  • 15,395
  • 32
  • 113
  • 196
Oleh Prypin
  • 33,184
  • 10
  • 89
  • 99
  • Well, if Python 2 libraries aren't being ported to Python 3, let's transform Python 2 into Python 3........ `from __future__ import unicode_literals,division,print_function`, etc. – Oleh Prypin Jul 29 '11 at 09:16
  • 18
    @Jbernardo, Tkinter is not as bad as it used to be, and you can make very nice GUIs with it. It uses the system's controls and themes better than previous versions did. You may have to work with the margins and padding a little to get better spacing. This is one thing PyQt does better right off the bat. PyQt is also a great library (and PyGTK), but you can still produce nice interfaces with Tkinter. – Todd Jul 29 '11 at 14:21
  • 13
    @JBernado: ugly is subjective, and a little out of touch with modern tk with themed widgets. Plus, most apps frankly don't need a lot of visual pizazz - functionality and/or ease of development is more often the driving factor. – Bryan Oakley Jul 29 '11 at 14:29
  • @Bryan "functionality and/or ease of development" Dude, that's what PyQt is all about. Qt Designer is a great tool for interfaces – JBernardo Jul 29 '11 at 16:30
  • 3
    @BlaXpirit: Porting to Python 3 is a good idea. Do that! But it's way more complex than you indicate. Here's help! http://python3porting.com/ – Lennart Regebro Jan 06 '12 at 11:18
  • 9
    Suggesting PyQt against Tkinter is like suggesting Oracle against Sqlite. – Benji Mizrahi Apr 16 '13 at 12:20

4 Answers4

53

I have found the TkDocs tutorial to be very useful. It describes building Tk interfaces using Python and Tkinter and ttk and makes notes about differences between Python 2 and 3. It also has examples in Perl, Ruby and Tcl, since the goal is to teach Tk itself, not the bindings for a particular language.

I haven't gone through the whole thing from start to finish, rather have only used a number of topics as examples for things I was stuck on, but it is very instructional and comfortably written. Today reading the intro and first few sections makes me think I will start working through the rest of it.

Finally, it's current and the site has a very nice look. He also has a bunch of other pages which are worth checking out (Widgets, Resources, Blog). This guy's doing a lot to not only teach Tk, but also to improve people's understanding that it's not the ugly beast that it once was.

nbro
  • 15,395
  • 32
  • 113
  • 196
Todd
  • 5,999
  • 2
  • 21
  • 21
  • My bone with this tutorial is that the examples in the tutorial look, for a lack of a better word, ugly. If one wants to dispell the myth that tk apps look not nice shouldnt one teach ppl how to do it right? – lalala Mar 06 '19 at 17:42
20

I recommend the NMT Tkinter 8.5 reference.

The module names used in some examples are those used in Python 2.7.
Here's a reference for the name changes in Python 3: link

One of the conveniences of ttk is that you can choose a preexisting theme,
which is a full set of Styles applied to the ttk widgets.

Here's an example I wrote (for Python 3) that allows you to select any available theme from a Combobox:

import random
import tkinter
from tkinter import ttk
from tkinter import messagebox

class App(object):

    def __init__(self):
        self.root = tkinter.Tk()
        self.style = ttk.Style()
        available_themes = self.style.theme_names()
        random_theme = random.choice(available_themes)
        self.style.theme_use(random_theme)
        self.root.title(random_theme)

        frm = ttk.Frame(self.root)
        frm.pack(expand=True, fill='both')
    # create a Combobox with themes to choose from
        self.combo = ttk.Combobox(frm, values=available_themes)
        self.combo.pack(padx=32, pady=8)
    # make the Enter key change the style
        self.combo.bind('<Return>', self.change_style)
    # make a Button to change the style
        button = ttk.Button(frm, text='OK')
        button['command'] = self.change_style
        button.pack(pady=8)

    def change_style(self, event=None):
        """set the Style to the content of the Combobox"""
        content = self.combo.get()
        try:
            self.style.theme_use(content)
        except tkinter.TclError as err:
            messagebox.showerror('Error', err)
        else:
            self.root.title(content)

app = App()
app.root.mainloop()

Side note: I've noticed that there is a 'vista' theme available when using Python 3.3 (but not 2.7).

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
  • 3
    The NMT reference is an ***Excellent*** resource. I keep the _tkinter_ [pdf version](http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf) open whenever I am working with `tkinter` – Christopher Pearson May 15 '15 at 19:32
  • 1
    NMT seems to have stopped hosting the tkinter reference. [PDF](https://web.archive.org/web/20190203155416/http://infohost.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf) – Honest Abe Sep 08 '19 at 01:53
3

I recommend reading the documentation. It is simple and authoritative, and good for beginners.

vy32
  • 28,461
  • 37
  • 122
  • 246
0

It's not really fresh but this is concise, and from what I've seen valid either for Python 2 and 3.

nbro
  • 15,395
  • 32
  • 113
  • 196
Carel
  • 3,289
  • 2
  • 27
  • 44