This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: tkinter docs: errors in A Simple Hello World Program
Type: enhancement Stage: resolved
Components: Tkinter Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: daniellovell, miss-islington, serhiy.storchaka, xtreak
Priority: normal Keywords: patch

Created on 2018-10-28 01:04 by daniellovell, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
tkinter_hello_world_issue.png daniellovell, 2018-10-28 01:04 Shows a simple, common alteration that throws error if root is not in global namespace
Pull Requests
URL Status Linked Edit
PR 10160 merged daniellovell, 2018-10-28 01:15
PR 10241 merged miss-islington, 2018-10-30 14:56
PR 10242 merged miss-islington, 2018-10-30 14:56
Messages (6)
msg328669 - (view) Author: Daniel Lovell (daniellovell) * Date: 2018-10-28 01:04
In the documentation for tkinter, "A Simple Hello World Program" Application class does not hold onto the master Tk() instance as a class attribute. This is a good practice, and newcomers to tkinter would likely have trouble closing the window without this since root will almost never be in the global namespace.

The original example:

"""""

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=root.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

""""

The proposed fix:

""""
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")
        
def main():
    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()

if __name__ == "__main__":
    main()

""""
msg328680 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-10-28 06:02
Thanks for the report. So the current example in the docs works fine since root is in global namespace. But this seems to be a sensible change to use self.master which references root instead of relying on root to be global though I don't know we should restructure the example to use main(). I am adding Serhiy for review.
msg328682 - (view) Author: Daniel Lovell (daniellovell) * Date: 2018-10-28 07:02
Thanks for the reply xtreak. I agree that changing the example to include main() isn't necessary - I unintentionally included that from my example of the case where the current version isn't functional. 

In the PR I submitted on Github (https://github.com/python/cpython/pull/10160), the proposed change is simply adding the master Tk instance to self.master then using that to close the window in the Quit button callback.

Sorry for the confusion.
msg328926 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-30 14:56
New changeset a80af770870937271865b5e2b05a2cfe40b024b6 by Serhiy Storchaka (Daniel Lovell) in branch 'master':
bpo-35086: Fix tkinter example "A Simple Hello World Program". (GH-10160)
https://github.com/python/cpython/commit/a80af770870937271865b5e2b05a2cfe40b024b6
msg328930 - (view) Author: miss-islington (miss-islington) Date: 2018-10-30 15:34
New changeset f51ef51db686938486bff453e791a3093a1df108 by Miss Islington (bot) in branch '3.7':
bpo-35086: Fix tkinter example "A Simple Hello World Program". (GH-10160)
https://github.com/python/cpython/commit/f51ef51db686938486bff453e791a3093a1df108
msg328931 - (view) Author: miss-islington (miss-islington) Date: 2018-10-30 15:35
New changeset c843a47007293d8361d0bfd45bfd7169afaa601c by Miss Islington (bot) in branch '3.6':
bpo-35086: Fix tkinter example "A Simple Hello World Program". (GH-10160)
https://github.com/python/cpython/commit/c843a47007293d8361d0bfd45bfd7169afaa601c
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79267
2018-10-30 15:40:19serhiy.storchakasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.6
2018-10-30 15:35:05miss-islingtonsetmessages: + msg328931
2018-10-30 15:34:58miss-islingtonsetnosy: + miss-islington
messages: + msg328930
2018-10-30 14:56:49miss-islingtonsetpull_requests: + pull_request9555
2018-10-30 14:56:39miss-islingtonsetpull_requests: + pull_request9554
2018-10-30 14:56:12serhiy.storchakasetmessages: + msg328926
2018-10-28 07:02:45daniellovellsetmessages: + msg328682
2018-10-28 06:02:41xtreaksetnosy: + serhiy.storchaka, xtreak
messages: + msg328680
2018-10-28 01:15:16daniellovellsetkeywords: + patch
stage: patch review
pull_requests: + pull_request9484
2018-10-28 01:04:56daniellovellcreate