Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tkinter docs?

74 views
Skip to first unread message

Rob Cliffe

unread,
May 23, 2023, 11:12:29 PM5/23/23
to
I have recently started converting a large project to tkinter, starting
with zero knowledge of tkinter.  (You are free to think: BAD IDEA. 😁)
I am well aware that adopting a new tool always involves a learning
curve, and that one is prone to think that things are more difficult
than they are/should be, and to belly-ache about it, and that in a year
from now I'll probably wonder what I'm fussing about.
Nonetheless ISTM that there is a particular problem with tkinter:

    There doesn't seem to be any decent documentation for it anywhere.

 I seem to have spent al least 95% (feels more like 99%) of my time in
research, only the rest available for coding.  Everything I've learned
has come from scouring the Internet for Stack Overflow pages, videos,
and other articles.  And while I'm VERY grateful for these resources,
most of them cover very basic use, and if I want information on some
more obscure technical point, I can only go on looking and looking and
hope I eventually stumble upon what I'm looking for, or some acceptable
substitute.
FWIW: The tkinter error messages are sometimes helpful, sometimes not,
occasionally very helpful (as when I pass an invalid option parameter to
a function and it tells me what the valid ones are).  I feel there is
plenty of room for improvement.

One example for those familiar with tkinter (I assure you there are
others) of how I struggle without adequate documentation:
    I had learned very early on that tkinter provides two versions of
some classes: the original, or "tk" versions, and a later generation,
the "ttk" versions (and I have to say that that was a turn-off, as it
seemed that the learning curve has just got steeper).  It was surely not
appropriate for me to delve deeply into this issue at that stage, as
there were so many other things I didn't know (like, practically
everything).  I decide to go with the "tk" versions pro tem, and to
investigate the "ttk" versions later if it seemed like a good idea.  One
annoyance is that some webpages are relevant to one version, some to the
other, almost always without being explicit about which.  (Can't blame
the "tk" ones, as they were probably written before "ttk" was invented.)
    I was writing a subclass of the Checkbutton class (tkinter's name
for what I call a checkbox).  I needed to be able to (1) set (2) clear
(3) interrogate the checked state.  This is easy to do if you associate
a "variable" with each Checkbutton instance, as seems to be usual
tkinter practice.  ("variable" is not a variable in the usual sense, but
an object provided by tkinter that is linked to a GUI object (such as a
Checkbutton), so that when the GUI object changes, the value of the
"variable" changes, and vice versa.) However, I decided that I wanted to
dispense with the variable, if possible.  (My motivation?  Simpler
code.  Shorter code.  Avoiding using up resources by creating
unnecessary objects.  You are free to think that I was misguided.  You
are free to think that I should have been happy with something that
worked.)  I didn't care whether I used a tk.Checkbutton or a
ttk.Checkbutton.
    From various Internet articles I discovered (slowly, after wading
through many articles that DID use a "variable"):
        A way of GETting the state of a tk.CheckButton (but not a
ttk.CheckButton)
        A way of SETting the state of a ttk.CheckButton (but not a
tk.CheckButton)
        Or the other way round.  Or something else.  I can no longer
remember, and I didn't keep a record of all my trials and tribulations,
and I can no longer trace which articles I read when.
    EVENTUALLY I discovered:
        For a ttk.CheckButton (only), where cb is the checkbox
            cb.state() returns a tuple of strings which contains, or
doesn't, "selected", according to the checked state
            cb.state(["selected"]) sets the checked state
            cb.state(["!selected"]) clears the checked state
"Aha!" I thought.  "Problem solved".  Gleefully I wrote my code and
tested it.
Er, well, not quite.  When the Checkbutton object was instantiated, it
showed neither a checked nor an unchecked box, but a solid black box. 
This was the third or so-called "alternate" state provided by tkinter. 
(Gee, thanks.  Oh sorry, it's probably not your fault; as I understand
it, tkinter, you're really just a wrapper.)
Further research revealed that I could get past this by writing
    cb.state(["!alternate", "!selected"])
when the object was instantiated.
Phew!  Finally got my code working.
But the story is not quite over.  To get the checked state I was using
    "selected" in cb.state()
While this works, later (in fact while composing this e-mail) I stumbled
across
    cb.instate(["selected"])
which does the same thing but is, I guess, the preferred method.  I have
amended my code accordingly.
(I don't know why the square brackets are necessary, but
    cb.instate("selected")
doesn't work.)
Sigh.  I suppose I have the satisfaction of having overcome a
challenge.  But how much nicer if all this information were in one place
somewhere.

Now, my remarks are as a tk newbie; they may be naive and ignorant,
perhaps even laughable.  But IMO it is often worth listening to input
from newbies to consider how things might be improved.

Comments, anyone?
Better yet (holds breath ...) can anyone point me towards some decent
tkinter documentation?

Best wishes
Rob Cliffe

Grant Edwards

unread,
May 23, 2023, 11:28:08 PM5/23/23
to
On 2023-05-24, Rob Cliffe via Python-list <pytho...@python.org> wrote:
> I have recently started converting a large project to tkinter, starting
> with zero knowledge of tkinter.  (You are free to think: BAD IDEA. 😁)

Well, you could be translating them to Tcl/Tk -- so on the scale of
bad ideas, your's barely registers.

> I am well aware that adopting a new tool always involves a learning
> curve, and that one is prone to think that things are more difficult
> than they are/should be, and to belly-ache about it, and that in a year
> from now I'll probably wonder what I'm fussing about.
> Nonetheless ISTM that there is a particular problem with tkinter:
>
> There doesn't seem to be any decent documentation for it anywhere.

Back in the day, the Grayson book was the definitive reference:

https://www.amazon.com/Python-Tkinter-Programming-John-Grayson/dp/1884777813/

It's 20+ years old now, so the version of Python it uses is pretty
ancient, and I presume Tk has also changed a bit over the years, so...

There are a couple recent books, but I haven't looked at them:

https://www.amazon.com/Modern-Tkinter-Busy-Python-Developers-dp-1999149564/dp/1999149564/

https://www.amazon.com/Python-GUI-Programming-Tkinter-user-friendly/dp/1801815925/

--
Grant

aapost

unread,
May 24, 2023, 12:02:24 AM5/24/23
to
On 5/23/23 21:18, Rob Cliffe wrote:

> Comments, anyone?
> Better yet (holds breath ...) can anyone point me towards some decent
> tkinter documentation?

The variables are slightly more integrated when using tcl/tk directly,
python has to have the object so you can track/use them easier. And the
variables are more of what is intended to track a widgets purpose, vs
state stuff that is more the state of the displaying of the widget.

occasionally giving tcl/tk a try directly helps, and using the official
tcl/tk docs in addition to a couple other decent docs below.

https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/index.html
https://tkdocs.com/tutorial/styles.html

https://www.tcl.tk/man/tcl8.6/
https://www.tcl.tk/man/tcl8.6/TkCmd/contents.html
https://www.tcl.tk/man/tcl8.6/TkLib/contents.html

(this used to be some documentation that I only discovered existed
recently, but most of it doesn't load and I don't know that anyone
backed it up for rehosting..)
https://web.archive.org/web/20200227170827/http://effbot.org/tkinterbook

tk is decent for what it can do, (and it could do potentially more if
you had a lifetime to dedicate to it, lol) and it's reasonably stable
and not being perpetually hacked about and broken like gtk.

A couple additional pointers in case these haven't been figured out yet:
Running it interactively and using tab complete helps.
If you are running an app you can comment out the mytk.mainloop()
statement then run python -i yourtk.py to give you access to all widgets
live.
For navigating them, just come up with a widget naming convention that
works for you in a similar vein to how tk does (like .frame.frame.label
to start),
i.e.
main = tk.Tk()
main.frame = tk.Frame()
That way if you have anchored your widgets to something at the root
level you can navigate down to whatever you are looking for easily. (you
can also use winfo_children() iteratively to travel the way tcl
structures them, but that is tedious).

Chris Angelico

unread,
May 24, 2023, 2:40:31 AM5/24/23
to
On Wed, 24 May 2023 at 13:11, Rob Cliffe via Python-list
<pytho...@python.org> wrote:
>
> I have recently started converting a large project to tkinter, starting
> with zero knowledge of tkinter. (You are free to think: BAD IDEA. 😁)
> I am well aware that adopting a new tool always involves a learning
> curve, and that one is prone to think that things are more difficult
> than they are/should be, and to belly-ache about it, and that in a year
> from now I'll probably wonder what I'm fussing about.

Yes, I do think this is a bad idea, though not an abysmal one. I would
recommend first playing with tkinter in a dedicated UI-only project
before converting the main project to it. Your code in the scratch
project can be as messy as it likes, and then you learn from it before
tackling the big one :)

But your concerns about tkinter's documentation are, sadly,
well-founded. Since it's this weird system of thin wrappers around
Tcl/Tk, a lot of things are basically just "go read the Tk docs".
There are a few key concepts you'll need to get your head around, and
I can't go into details because I never truly got *my* head around
them... but mostly, the way that variables are used and what happens
when events fire.

Good luck with it. I'll be frank, building a GUI can be pretty hard...
I'm still unsure whether the best way is to use something like
tkinter, or to build a web app, pop up a browser window, and establish
a websocket to communicate between your JavaScript front end and your
Python back end. Yeah, that's how bad GUI building can be sometimes -
it is potentially *easier* to build two halves of your app in two
different languages than to make your GUI work the way you want it.

ChrisA

Christian Gollwitzer

unread,
May 24, 2023, 2:51:32 AM5/24/23
to
Am 24.05.23 um 03:18 schrieb Rob Cliffe:
> I have recently started converting a large project to tkinter, starting
> with zero knowledge of tkinter.  (You are free to think: BAD IDEA. 😁)

Welcome to the awesome world of GUI development.

>     I was writing a subclass of the Checkbutton class (tkinter's name
> for what I call a checkbox).  I needed to be able to (1) set (2) clear
> (3) interrogate the checked state.  This is easy to do if you associate
> a "variable" with each Checkbutton instance, as seems to be usual
> tkinter practice.  ("variable" is not a variable in the usual sense, but
> an object provided by tkinter that is linked to a GUI object (such as a
> Checkbutton), so that when the GUI object changes, the value of the
> "variable" changes, and vice versa.) However, I decided that I wanted to
> dispense with the variable, if possible.

As you found out the hard way, it is possible, but then you dive into
the internals of how the widgets work - usually you don't want to know
that. Also, this bit differs between Tk and Ttk widgets.


> [...] lengthe description of Checkbutton internals


In GUI programming, you will meet a bag of concepts that you haven't
seen in sequential programming before. To make it worse, every GUI
toolkit brings its own new set of concepts to the table. Maybe it is
helpful to work through a Tk tutorial first. This is a good one:

http://tkdocs.com/tutorial/index.html

Similarly to the tutorial I would suggest to stick with the ttk widgets.
Those are an "update" (> 10 years ago) to the tk widgets and are
supposed to provide sensible defaults matching the users experience with
native GUI elements. There are only 3 widgets where you must use a Tk
widget, this is Text (for multiline formatted text entry), Canvas (for
2D drawings), and Toplevel (for new windows/ popups etc.)

Christian

Cameron Simpson

unread,
May 24, 2023, 8:31:19 PM5/24/23
to
On 24May2023 02:18, Rob Cliffe <rob.c...@btinternet.com> wrote:
>    There doesn't seem to be any decent documentation for it anywhere.

Already mentioned in the replies, I use this:
https://tkdocs.com/shipman/index.html
quite a lot.

Dan Kolis

unread,
May 27, 2023, 11:12:16 AM5/27/23
to
The below is a copy and paste from my home made TKINTER Cheat sheets on Canvas.

This is an addenda, more generalised. below are main line items

CLICK ON ME AND LOOK YOURSELF:
https://curlie.org/Computers/Programming/Languages/Tcl-Tk

Chris

----------------------------------------------------------------------------------------------------------------

Im making a IDE of TKINTER and have vast resources available for that. If you are doing something serious and esp if you can throw in on labor, tell me that, right here then we can xchange more 1:1 email id, whatever.

Rob Cliffe maybe say somewhat more abotu your efforts to date, what do you have so far what is a deliverable ?

My project is an IDE for synthetic biology.
Daniel B. Kolis Toronto / Ont Can


Good luck, needy dudes for info. Just keep working and learning. TKINTER is pretty seriously usable no doubt for most projects.

More reading – URLS

Tcl Developer Xchange
Was Scriptics.com, firm founded by Tcl creator, and original sole author, Professor John Ousterhout.
Advocacy, tutorials, documentation, links, news, software, latest releases, downloads.

The Entropy Liberation Front
Providing a collection of Tcl/Tk plugins and sources as well as links to other resources.

ActiveTcl
ActiveState's distribution of Tcl, available for Linux, Solaris, HP-UX and Windows. Overview,
features, resources and testimonials.

Tcl/Tk Extensions and Information Page
Extensions to make Tcl/Tk applications easy to implement. Some of these extensions are specific to
NT, while others run in both, Unix and NT environments.

Tcl Tutorial
This tutorial is aimed at those who have some knowledge of programming, although you don't have to
be an expert. The tutorial is intended as a companion to the Tcl manual pages which provide a reference
for all Tcl commands.

Tcl Java Integration
About Jacl, a Java implementation of Tcl 8.x., and TclBlend, a package for Tcl 8.x to load and interact
with the Java VM.

A Tcl(Tutorial for Cool Language) for Tcl/Tk Tutorial
A Tcl/Tk tutorial aimed at beginners.
dgWebHelp: Tcl/Tk
Fast access to Tcl/Tk and extensions manual pages via a nice Java applet.
Wikipedia's Tcl Tutorial

A short tutorial on the Tcl language from Wikipedia.
Tek-Tips - Tcl/Tk
A WWW based discussion forum for Tcl/Tk programmers.
Scripting Graphical Commands with Tcl/Tk Mini-HOWTO
A quick introduction by example to creating graphical user interfaces for command line applications
with Tcl and Tk.

Tcl for Web Nerds
A guide programming a Web service using TCL as the CGI language.
Using Tcl/Tk in Multimedia Applications
A short tutorial about how to use Tcl/Tk in Multimedia applications for X.
Donal K. Fellows's Tcl Archive
Featuring extended Tcl fontification for Emacs, scripts and extensions, Tclets and Games.
Tcl/Tk Quick Reference
For Tcl/Tk 8.x based on the quick reference guide written for Perl by Johan Vromans. A Perl script is
included in the distribution to reorganize the 52 pages in the Postscript source into booklet form.
Tcl-Wear Chronology
A history of shirts, hats, office toys and other Tcl-related paraphernalia.
Practical Programming in Tcl and Tk
Fifteen out of 48 chapters of Brent Welch's book.
Tcl/Tk SourceForge Project
The core development home for Tcl and the Tk toolkit.
Avia Training and Consulting
Provides services for the Tcl programming language and extensions such as Tk and Expect.
Page 47CANVAS-TK-CC-1 – 26 May 2023 – Daniel B. Kolis
Clif Flynt
Homepage of "Tcl/Tk For Real Programmers" author. Professional and personal information. Book
overview, table of Contents, errata. Tcl/Tk related items.
Tcl/Tk advocacy
Teaches non-programming types how to create GUI applications using Tcl/Tk. Uses Windows, Cygwin,
web browser or Linux as platform for learning, practicing, experimenting.
TclTutor
Interactive computer aided instruction package to learn Tcl. It consists of 43 lessons covering all basic
Tcl commands.

USE THE MACHINE READABLES FOR THESE AS ABOVE

Daniel B. Kolis

Source:
https://curlie.org/Computers/Programming/Languages/Tcl-Tk

my ref: Scraped by Daniel B. Kolis 27 May 2023, https://groups.google.com/g/comp.lang.python/c/eOFMg8t5kjA

Rob Cliffe

unread,
May 30, 2023, 1:28:04 PM5/30/23
to
Thanks to everyone who replied.  All replies were constructive, none
were telling me to stop belly-aching.
I forgot/omitted to state that it was I who wrote the original project
(in a completely different language), making the task of re-writing it
much less formidable.  And meaning that I am familiar with the general
concepts of building a GUI.  Still, it will be a lot of work.

Grant, I may well buy one of the books you suggested.
I find the topic of themes and styles the hardest one to get my head
around (if anyone knows a good introduction that would be fantastic). 
All the other stuff is OK provided I can find something on the net to
give me the necessary information, which so far I usually can.

Christian, I am adopting your suggestion of using ttk widgets, except
for Button objects, because
    The colour of tk.Button objects can be set directly (bg=... fg=...)
    On my platform (Windows10) the shadowing of tk.Button objects is
more conspicuous (without using styles or whatever).

Best wishes
Rob Cliffe

Dan Kolis

unread,
May 30, 2023, 1:48:52 PM5/30/23
to
On Tuesday, May 30, 2023 at 1:28:04 PM UTC-4, Rob Cliffe wrote:
> Thanks to everyone who replied. All replies were constructive, none
> were telling me to stop belly-aching.

Hi, Dan says:
When you get your style ideas sort of frozen, maybe you can poke up a sample here.

Aworked example for yourself is useful anyway.

My stuffs all in Ubuntu so seeing what works and doesn't in MS products interests me a lot.

I usually avoid the tkk variations almost completely. I have all the things I use each wrapped in a call that does what I want both aesthetically and so on. My app is only medium sensitive to art look and feel, productivity to maintain it is more important then super fancy looking.

Regards,
Dan


Grant Edwards

unread,
May 30, 2023, 4:10:33 PM5/30/23
to
On 2023-05-26, Rob Cliffe via Python-list <pytho...@python.org> wrote:

> Grant, I may well buy one of the books you suggested.

I haven't had look at either of the newer books, but I got a lot of
good out of the Grayson book (20 years ago). I also had a Tcl/Tk book
that I found useful even when usng tkinter, but it's even older than
the Grayson one...

Dan Kolis

unread,
Jun 3, 2023, 11:11:54 AM6/3/23
to

This 780 paper page faces. more info at:
https://www.torontopubliclibrary.ca/detail.jsp?Entt=RDM2538548&R=2538548

Titled: "Tcl and the Tk toolkit", is pretty definitive.

I say:
Struggling to keep a TKINTER project all single threaded for really abstract work, in probably impossible.

Its *not* super hard to reach around it some .. into real TCL. but not to be trivialised, either.

Of course, only piss-ants use MS for anything involving development. Its nearly impossible to do serious work in MS really. Making the required final target system(s) for final projects in: linux, apple and MS, is pretty distracting, difficult, and annoying. It's possible, though

If you like piss and ants everyday instead of completing tasks, those are the two main ingredients to fuel the existence of Microsoft.

in Ubuntu this makes the screen shot attached:

Here's the program to run TCL directly with this one file test program:
tclsh npa-1029.tcl

And Here's the program:
---------------------------------------------------
package require Tk 8.0

wm title . "TKinter is made from TCL"
wm geometry . 450x350
pack [ button .b -text "click me" -command makeWindow ]
set counter 0

proc makeWindow {} {
# Make a unique widget name
global counter
set w .gui[ incr counter ]

# Make the toplevel
toplevel $w
wm title $w "Pretty easy"

# Put a window in it
pack [ label $w.xmpl -text "Good luck with your projects !" ]
pack [ button $w.ok -text OK -command [ list destroy $w ] ]
--------------------------------------------

I hope this adds some mini fact set to: "How's this really work, anyway ?"

Every super graphics, talks tand listens, etc full motion app released in linux is X11. This is way beyond TKINTER. Though TKINTER is a fine, fine thing, its not the whole thing.

If you decide this matters to you, TKINTER with bonused up X11 support around it, matters to me. Tell me what you need, maybe we can find some common sharable work. Python <-> TCL <-> X11 obviously.

Try the above and feel liberated. Probably possible in MS by applying some handful of hours, in linux, its 45 seconds to do it. Who knows what mysteries live iunder the porch at Apple-landers.

Regards,
Daniel B. Kolis / 03 Jun 2023

0 new messages