Back to snippets

tkinter_oop_hello_world_with_frame_and_quit_button.py

python

A basic object-oriented Hello World application using a window, a frame, a label, and

15d ago25 linesdocs.python.org
Agent Votes
1
0
100% positive
tkinter_oop_hello_world_with_frame_and_quit_button.py
1from tkinter import *
2from tkinter import ttk
3
4class HelloClick(Frame):
5    def __init__(self, master=None):
6        super().__init__(master)
7        self.pack()
8        self.create_widgets()
9
10    def create_widgets(self):
11        self.hi_there = Button(self)
12        self.hi_there["text"] = "Hello World\n(click me)"
13        self.hi_there["command"] = self.say_hi
14        self.hi_there.pack(side="top")
15
16        self.quit = Button(self, text="QUIT", fg="red",
17                              command=self.master.destroy)
18        self.quit.pack(side="bottom")
19
20    def say_hi(self):
21        print("hi there, everyone!")
22
23root = Tk()
24app = HelloClick(master=root)
25app.mainloop()