You need your "constructor" method to be named __init__, not _init_. As it is written, your grid and create_widgets methods never get called since _init_ never gets called.

Answer from mgilson on Stack Overflow
🌐
Real Python
realpython.com β€Ί python-gui-tkinter
Python GUI Programming: Your Tkinter Tutorial – Real Python
December 7, 2024 - Complete an interactive tutorial for Python's GUI library Tkinter. Add buttons, text boxes, widgets, event handlers, and more while building two GUI apps.
🌐
Python Basics
pythonbasics.org β€Ί home β€Ί tkinter β€Ί tkinter buttons (gui programming)
Tkinter buttons (GUI Programming) - pythonbasics.org
from tkinter import * class Window(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.master = master # widget can take all window self.pack(fill=BOTH, expand=1) # create button, link it to clickExitButton() exitButton = Button(self, text="Exit", command=self.clickExitButton) # place button at (0,0) exitButton.place(x=0, y=0) def clickExitButton(self): exit() root = Tk() app = Window(root) root.wm_title("Tkinter button") root.geometry("320x200") root.mainloop()
🌐
Python GUIs
pythonguis.com β€Ί tutorials β€Ί getting started with tkinter β€Ί create buttons in tkinter
Button Widgets in Tkinter
July 13, 2022 - In this tutorial, we will learn how to make use of buttons in our Tkinter applications using the Button widget. By the end of this tutorial, you will be able to include buttons in your Tkinter GUIs, hook these buttons up to Python functions to make things happen and learn how to customize them ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python-creating-a-button-in-tkinter
Python Tkinter - Create Button Widget - GeeksforGeeks
August 14, 2024 - Stepwise implementation: Step 1: ... = Tk()). Python3 # Import package and it's modules from tkinter import * # create root window root = Tk() # roo ... Prerequisite: Creating a button in tkinter Tkinter is the most commonly used library for developing GUI (Graphical User ...
🌐
Tutorialspoint
tutorialspoint.com β€Ί python β€Ί tk_button.htm
Tkinter Button
You can attach a function or a method to a button which is called automatically when you click the button. Here is the simple syntax to create this widget βˆ’ ... options βˆ’ Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas. Following are commonly used methods for this widget βˆ’ ... from tkinter import * from tkinter import messagebox top = Tk() top.geometry("100x100") def helloCallBack(): msg=messagebox.showinfo( "Hello Python", "Hello World") B = Button(top, text ="Hello", command = helloCallBack) B.place(x=50,y=50) top.mainloop()
Top answer
1 of 2
5

You need your "constructor" method to be named __init__, not _init_. As it is written, your grid and create_widgets methods never get called since _init_ never gets called.

2 of 2
2

OK, first problem is that you have declared your following code:

root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root)

root.mainloop()code here

inside the class itself. It should be outside, so this an indentation problem (maybe stackoverflow problem with indents?).

secondly I simplified the code to get it to run

from Tkinter import * 

class Application(Frame):
      """A GUI application with three button"""

     #create a class variable from the root (master):called by the constructor
     def _init_(self, master):
          self.master = master

     #simple button construction
     # create a button with chosen arguments
     # pack it after the creation not in the middle or before

     def create_widgets(self):
          #"""Create three buttons"""
          #Create first button
          btn1 = Button(self.master, text = "I do nothing")
          btn1.pack()

          #Create second button
          btn2 = Button(self.master, text = "T do nothing as well")
          btn2.pack()

         #Create third button
         btn3=Button(self.master, text = "I do nothing as well as well")
         btn3.pack()

  #must be outside class definition but probably due to stackoverlow
  root = Tk()
  root.title("Lazy Button 2")
  root.geometry("500x500")
  app = Application(root)
  #call the method
  app.create_widgets()
  root.mainloop()

This is a starting point and definitely works as proven below:

You can probablty muck around with the grid() instead of pack and call the method from the def init constructor. Hope it helps.

This calling method also works:

root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root).create_widgets()  #creates and invokes
root.mainloop()

My final try also works:

def __init__(self,master):
    self.master = master
    self.create_widgets()

followed by:

root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root)
root.mainloop()

The final code:

from Tkinter import * 

class Application(Frame):
"""A GUI application with three button"""

def __init__(self,master):
    self.master = master
    self.create_widgets()



def create_widgets(self):
    #"""Create three buttons"""
    #Create first buttom
    btn1 = Button(self.master, text = "I do nothing")
    btn1.pack()

    #Create second button
    btn2 = Button(self.master, text = "T do nothing as well")
    btn2.pack()

    #Create third button
    btn3=Button(self.master, text = "I do nothing as well as well")
    btn3.pack()

root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root)
root.mainloop()

🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί creating-gui-in-python-labels-buttons-and-message-box
Creating GUI in Python - Labels, Buttons, and Message Box - GeeksforGeeks
July 23, 2025 - import tkinter as tk from tkinter import messagebox def show_message(): messagebox.showinfo("Message", "Hello, this is a message!") # Create the main application window root = tk.Tk() root.title("Message Box Example") # Create a button to trigger the message box button = tk.Button(root, text="Show Message", command=show_message) button.pack() # Run the application root.mainloop() Output: Comment Β· Article Tags: Article Tags: Python Β· Python-tkinter Β· Python-gui Β·
🌐
Javatpoint
javatpoint.com β€Ί python-tkinter-button
Python Tkinter Button - Javatpoint
Python Tkinter Button with python tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, basics, data types, operators, etc.
Find elsewhere
🌐
Python Tutorial
pythontutorial.net β€Ί home β€Ί tkinter tutorial β€Ί tkinter button
Tkinter Button - Python Tutorial
April 3, 2025 - button = ttk.Button( master, text=label, command=fn )Code language: Python (python) In this syntax: The text determines the label of the button. The command specifies a callback function that will execute automatically when you click the button.
🌐
Python Course
python-course.eu β€Ί tkinter β€Ί buttons-in-tkinter.php
3. Buttons in Tkinter | Tkinter | python-course.eu
The Button widget is a standard Tkinter widget, which is used for various kinds of buttons. A button is a widget which is designed for the user to interact with, i.e. if the button is pressed by mouse click some action might be started. They can also contain text and images like labels.
🌐
Python Guides
pythonguides.com β€Ί python-tkinter-button
How To Create Buttons In Python With Tkinter?
March 19, 2025 - The Tkinter Button widget is a graphical control element used in Python’s Tkinter library to create clickable buttons in a graphical user interface (GUI).
Top answer
1 of 6
20

Overview

No, you don't have to "draw a rect, then make a loop". What you will have to do is import a GUI toolkit of some sort, and use the methods and objects built-in to that toolkit. Generally speaking, one of those methods will be to run a loop which listens for events and calls functions based on those events. This loop is called an event loop. So, while such a loop must run, you don't have to create the loop.

Caveats

If you're looking to open a window from a prompt such as in the video you linked to, the problem is a little tougher. These toolkits aren't designed to be used in such a manner. Typically, you write a complete GUI-based program where all input and output is done via widgets. It's not impossible, but in my opinion, when learning you should stick to all text or all GUI, and not mix the two.

Example using Tkinter

For example, one such toolkit is tkinter. Tkinter is the toolkit that is built-in to python. Any other toolkit such as wxPython, PyQT, etc will be very similar and works just as well. The advantage to Tkinter is that you probably already have it, and it is a fantastic toolkit for learning GUI programming. It's also fantastic for more advanced programming, though you will find people who disagree with that point. Don't listen to them.

Here's an example in Tkinter. This example works in python 2.x. For python 3.x you'll need to import from tkinter rather than Tkinter.

import Tkinter as tk

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

        # create a prompt, an input box, an output label,
        # and a button to do the computation
        self.prompt = tk.Label(self, text="Enter a number:", anchor="w")
        self.entry = tk.Entry(self)
        self.submit = tk.Button(self, text="Submit", command = self.calculate)
        self.output = tk.Label(self, text="")

        # lay the widgets out on the screen. 
        self.prompt.pack(side="top", fill="x")
        self.entry.pack(side="top", fill="x", padx=20)
        self.output.pack(side="top", fill="x", expand=True)
        self.submit.pack(side="right")

    def calculate(self):
        # get the value from the input widget, convert
        # it to an int, and do a calculation
        try:
            i = int(self.entry.get())
            result = "%s*2=%s" % (i, i*2)
        except ValueError:
            result = "Please enter digits only"

        # set the output widget to have our result
        self.output.configure(text=result)

# if this is run as a program (versus being imported),
# create a root window and an instance of our example,
# then start the event loop

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
2 of 6
1

You should take a look at wxpython, a GUI library that is quite easy to start with if you have some python knowledge.

The following code will create a window for you (source):

import wx

app = wx.App(False)  # Create a new app, don't redirect stdout/stderr to a window.
frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.
frame.Show(True)     # Show the frame.
app.MainLoop()

Take a look at this section (how to create buttons). But start with the installation instructions.

🌐
Readthedocs
python-textbok.readthedocs.io β€Ί en β€Ί 1.0 β€Ί Introduction_to_GUI_Programming.html
Introduction to GUI programming with tkinter β€” Object-Oriented Programming in Python 1 documentation
So far we have only bound event handlers to events which are defined in tkinter by default – the Button class already knows about button clicks, since clicking is an expected part of normal button behaviour. We are not restricted to these particular events, however – we can make widgets listen for other events and bind handlers to them, using the bind method which we can find on every widget class. Events are uniquely identified by a sequence name in string format – the format is described by a mini-language which is not specific to Python.
🌐
Python Programming
pythonprogramming.net β€Ί tkinter-python-3-tutorial-adding-buttons
Python Programming Tutorials
We'll get there, but first let's just show the button. ... from tkinter import * class Window(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.master = master self.init_window() #Creation of init_window def init_window(self): # changing the title of our master widget self.master.title("GUI") # allowing the widget to take the full space of the root window self.pack(fill=BOTH, expand=1) # creating a button instance quitButton = Button(self, text="Quit") # placing the button on my window quitButton.place(x=0, y=0) root = Tk() #size of the window root.geometry("400x300") app = Window(root) root.mainloop()
🌐
Real Python
realpython.com β€Ί pysimplegui-python
PySimpleGUI: The Simple Way to Create a GUI With Python – Real Python
July 9, 2024 - PySimpleGUI uses nested Python lists to lay out its elements. In this case, you add a Text() element and a Button() element. Then you create the window and pass in your custom layout. The last block of code is the event loop. A graphical user interface needs to run inside a loop and wait for the user to do something. For example, the user might need to press a button in your UI or type something with their keyboard.
🌐
Like Geeks
likegeeks.com β€Ί home β€Ί python β€Ί python gui examples (tkinter tutorial)
Python GUI examples (Tkinter Tutorial)
Learn how to develop GUI applications ... GUI examples, you'll learn how to create a label, button, entry class, combobox, check button, radio button, scrolled text, messagebox, spinbox, ......
🌐
w3resource
w3resource.com β€Ί python-exercises β€Ί tkinter β€Ί python-tkinter-basic-exercise-6.php
Python Tkinter GUI program: Adding labels and buttons.
import tkinter as tk # Create the main window parent = tk.Tk() parent.title("Button Click Event Handling") # Create a label widget label = tk.Label(parent, text="Click the button and check the label text:") label.pack() # Function to handle button click event def on_button_click(): label.config(text="Button Clicked!") # Create a button widget button = tk.Button(parent, text="Click Me", command=on_button_click) button.pack() # Start the Tkinter event loop parent.mainloop()
🌐
Instructables
instructables.com β€Ί design β€Ί software
How to Create a Button and Handle Button Click Events in Tkinter (ttkbootstrap) With Python - Instructables
January 23, 2026 - How to Create a Button and Handle Button Click Events in Tkinter (ttkbootstrap) With Python: In this Instructable we’ll guide you through the process of creating a button in a Tkinter window, customizing its appearance using ttkbootstrap, and defining the actions to take when the button is clicked.