New submission from Daniel Lovell <lovell.danie...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue35086>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to