classification
Title: tkinter: problems with hello doc example
Type: behavior Stage: resolved
Components: Documentation, Tkinter Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: asvetlov, docs@python, ned.deily, python-dev, roger.serwy, terry.reedy
Priority: normal Keywords:

Created on 2012-03-01 00:00 by terry.reedy, last changed 2012-03-15 04:43 by asvetlov. This issue is now closed.

Files
File name Uploaded Description Edit
tk_hello.py terry.reedy, 2012-03-02 20:04 Revision 3 of Hello World example
Messages (10)
msg154669 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-03-01 00:00
After an intro, the tkinter docs start with a traditional 'Hello World'.
http://docs.python.org/py3k/library/tkinter.html#a-simple-hello-world-program

Problem 1. The example ends with 
root.destroy
John Salerno, today, on python-list thread "Is it necessary to call Tk() when writing a GUI app with Tkinter?", reported the following, which I verified. On Windows, at least, closing the unlabelled Tk window by clicking the normal [X} close button on the frame causes

_tkinter.TclError: can't invoke "destroy" command:  application has been destroyed

This does not happen if one instead clicks the QUIT button inside the frame (as that does not destroy the app).

Question 1a. Does the example work the same with current tcl/tk on *nix and mac?

I assume it should, but I do not know if the example was buggy from the beginning or if something changes. In any case, I do not want to commit a solution that is not tested on more than my Win7.

Question 1b. How do we most simply fix this part of the example?

In '''self.QUIT["command"] = self.quit''', I changed 'quit' to 'destroy' and the result is to destroy the widgets but leave the outer frame and continue the mainloop.

I tried another solution based on adding
        self.protocol("WM_DELETE_WINDOW", self.onDestroyWindow)
to __init__(), but while this worked for Rick Johnson's class App(tk.Tk) example in his response to Salerno, it does not work for this class Application(Frame) example as the latter has no .protocol attribute.
---

Problem 2. A 'Hello World' example should say 'Hello World' instead of just 'hello'. So I make the trivial edit and tk add braces and displays '{Hello World}'. What??? Adding '\n' does not trigger brace addition, so  "Hello_World\n(click_me)" works as expected.

Queston 2. How does one put a space in a button label with triggering addition of braces, or is this impossible?
---

Problem and Solution 3. The example currently puts a loud capital red QUIT button first, and the quiet lowercase blass hello button second. This is backwards in both placement and emphasis. Putting the expanded hello buttom on top and QUIT under looks better to me.
---

Problem 4 (in the intro, before the example): There is a lot that could be changed or added to the doc, but I think revising 

to use Tkinter all you need is a simple import statement:
import tkinter
Or, more often:
from tkinter import *

(3.x version) to add what I think is the best option, 'import tkinter as tk', as at least equal to the '*' option, should be done soon. I am thinking of something like

to use Tkinter, use one of these import statements:
import tkinter
import tkinter as tk
from tkinter import *
msg154672 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-03-01 05:15
Rick Johnson anwered question 1b on python-list: simply move root.destroy to the button. I make other changes also: use import...as; show that options can be set in constructor call. File uploaded.
msg154787 - (view) Author: Roger Serwy (roger.serwy) * (Python committer) Date: 2012-03-02 19:13
I tried your tk_hello.py code and it works on Python 3.


The code for creating the QUIT button exceeds 80 columns. I'd insert a \n after the second argument:

        self.QUIT = tk.Button(self, text = "QUIT", 
                              fg = "red", command = root.destroy)
msg154789 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-03-02 19:27
Do you know anything about spaces in the button text causing the addition of {bra ces}? I would prefer not to have to use an underscore in Hello_World.

I fixed the long line and also updated pack calls pass options by name instead of a dict.
msg154790 - (view) Author: Roger Serwy (roger.serwy) * (Python committer) Date: 2012-03-02 19:40
Ah, that's subtle. There's a comma at the end of that ["text"] line which is making the string a one-element tuple. Removing it fixes the issue.
msg154791 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-03-02 20:04
Grrrr, another but in the original ;-)
Thank you! I will incorporate into the doc now.
msg155845 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-03-15 03:08
What the status of 'hello demo'?

If there are no objections I can update the docs.
msg155849 - (view) Author: Roger Serwy (roger.serwy) * (Python committer) Date: 2012-03-15 03:30
The current tk_hello.py file works under Ubuntu 11.04 with the latest python build from the repository. It looks good to me.
msg155855 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-03-15 04:41
New changeset d8ba959a547b by Andrew Svetlov in branch 'default':
Closes issue #14163 - tkinter: problems with hello doc example
http://hg.python.org/cpython/rev/d8ba959a547b
msg155856 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-03-15 04:43
Pushed into master. Thanks.
History
Date User Action Args
2012-03-15 04:43:23asvetlovsetstatus: open -> closed
resolution: fixed
messages: + msg155856

stage: patch review -> resolved
2012-03-15 04:41:37python-devsetnosy: + python-dev
messages: + msg155855
2012-03-15 03:30:32roger.serwysetmessages: + msg155849
2012-03-15 03:08:42asvetlovsetnosy: + asvetlov
messages: + msg155845
2012-03-02 20:04:55terry.reedysetfiles: - tk_hello.py
2012-03-02 20:04:45terry.reedysetfiles: + tk_hello.py

messages: + msg154791
2012-03-02 19:40:55roger.serwysetmessages: + msg154790
2012-03-02 19:27:49terry.reedysetfiles: - tk_hello.py
2012-03-02 19:27:28terry.reedysetfiles: + tk_hello.py

messages: + msg154789
2012-03-02 19:13:42roger.serwysetmessages: + msg154787
2012-03-01 05:15:51terry.reedysetfiles: + tk_hello.py

messages: + msg154672
stage: patch review
2012-03-01 00:00:47terry.reedycreate