[New-bugs-announce] [issue35086] tkinter docs: errors in A Simple Hello World Program

Daniel Lovell report at bugs.python.org
Sat Oct 27 21:04:56 EDT 2018


New submission from Daniel Lovell <lovell.daniel92 at gmail.com>:

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

""""

----------
components: Tkinter
files: tkinter_hello_world_issue.png
messages: 328669
nosy: NuclearLemon
priority: normal
severity: normal
status: open
title: tkinter docs: errors in A Simple Hello World Program
type: enhancement
versions: Python 3.7, Python 3.8
Added file: https://bugs.python.org/file47894/tkinter_hello_world_issue.png

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35086>
_______________________________________


More information about the New-bugs-announce mailing list