tkinter Hello, World! (modular, object-oriented)


import tkinter as tk

class HelloWorld(tk.Frame):
    def __init__(self, parent):
        super(HelloWorld, self).__init__(parent)

        self.label = tk.Label(self, text="Hello, World!")
        self.label.pack(padx=20, pady=20)
if __name__ == "__main__":
    root = tk.Tk()

    main = HelloWorld(root)
    main.pack(fill="both", expand=True)


Note: It's possible to inherit from just about any tkinter widget, including the root window. Inheriting from tkinter.Frame is at least arguably the most flexible in that it supports multiple document interfaces (MDI), single document interfaces (SDI), single page applications, and multiple-page applications.