Fresh tutorial on tkinter and ttk for Python 3

Python 3.xTkinterTtk

Python 3.x Problem Overview


Where can I find the most modern tutorial that teaches [tkinter][tkinter] together with [ttk][tkinter.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.

[tkinter]: http://docs.python.org/py3k/library/tkinter.html "tkinter — Python interface to Tcl/Tk" [tkinter.ttk]: http://docs.python.org/py3k/library/tkinter.ttk.html "tkinter.ttk — Tk themed widgets"

Python 3.x Solutions


Solution 1 - Python 3.x

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.

Solution 2 - Python 3.x

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).

Solution 3 - Python 3.x

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

Solution 4 - Python 3.x

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOleh PrypinView Question on Stackoverflow
Solution 1 - Python 3.xToddView Answer on Stackoverflow
Solution 2 - Python 3.xHonest AbeView Answer on Stackoverflow
Solution 3 - Python 3.xvy32View Answer on Stackoverflow
Solution 4 - Python 3.xCarelView Answer on Stackoverflow