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()
Answer from Bryan Oakley on Stack Overflow
๐ŸŒ
Python GUIs
pythonguis.com โ€บ tutorials โ€บ getting started with tkinter โ€บ create buttons in tkinter
Create Buttons 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 ...
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to make a button in python
How to make a button in Python | Replit
February 12, 2026 - The command parameter is the most direct way to make a button do something. You just assign it a function name, and Tkinter calls that function whenever the button is clicked. Notice the function name button_click is passed without parentheses.
Discussions

How to make a window with buttons in python - Stack Overflow
How do I create a function that makes a window with two buttons, where each button has a specified string and, if clicked on, returns a specified variable? Similar to @ 3:05 in this video https://www. More on stackoverflow.com
๐ŸŒ stackoverflow.com
class - Creating buttons with Python GUI - Stack Overflow
I am trying to create Buttons in Python with classes, but when running it the buttons do not appear. Following is my code #Button_2 #Using Classes from Tkinter import * class Application(Frame)... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How To Create a Button Input
It's not at all clear what you're asking for here. What are you trying to input? More on reddit.com
๐ŸŒ r/learnpython
8
15
October 1, 2024
Python tkinter button as input
You should be able to achieve this by making the callback of button assign that variable as whatever value you want. More on reddit.com
๐ŸŒ r/learnpython
4
2
August 12, 2021
๐ŸŒ
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()
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-creating-a-button-in-tkinter
Python Tkinter - Create Button Widget - GeeksforGeeks
August 22, 2025 - Creation of Button using tk themed widget (tkinter.ttk). This will give you the effects of modern graphics. Effects will change from one OS to another because it is basically for the appearance.
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.

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

๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3105495 โ€บ how-do-i-make-a-button-with-python
How do I make a button with python? | Sololearn: Learn to code for FREE!
#Tkinter Button. from tkinter import * # create a tkinter window root = Tk() # Open window having dimension 100x100 root.geometry('100x100') # Create a Button btn = Button(root, text = 'Click me !', bd = '5', command = root.destroy) # Set the ...
Find elsewhere
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ tkinter tutorial โ€บ tkinter button
Tkinter Button - Python Tutorial
April 3, 2025 - This is called the command binding in Tkinter. ... The master is the parent widget on which you place the button. The **kw is one or more keyword arguments you use to change the appearance and behaviors of the button. Here are the common configurations of the button widget: button = ttk.Button( master, text=label, command=fn )Code language: Python (python)
๐ŸŒ
Python Programming
pythonprogramming.net โ€บ tkinter-python-3-tutorial-adding-buttons
Python Programming Tutorials
The next major thing we see is the init_window() function in our window class. Here, we give the window a title, which adds the title of GUI. Then we pack, which allows our widget to take the full space of our root window, or frame. From there, we then create a button instance, with the text ...
๐ŸŒ
YouTube
youtube.com โ€บ codemy.com
Creating Buttons With TKinter - Python Tkinter GUI Tutorial #3 - YouTube
How to create buttons with TKinter and Python. In this video I'll show you how to create buttons with tKinter. Its pretty easy! In this series I'll show you ...
Published ย  January 14, 2019
Views ย  64K
๐ŸŒ
YouTube
youtube.com โ€บ watch
Create Modern Buttons With Tkinter in Python | Tkinter GUI ...
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
๐ŸŒ
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.
๐ŸŒ
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 Course
python-course.eu โ€บ tkinter โ€บ buttons-in-tkinter.php
3. Buttons in Tkinter | Tkinter | python-course.eu
import tkinter as tk def write_slogan(): print("Tkinter is easy to use!") root = tk.Tk() frame = tk.Frame(root) frame.pack() button = tk.Button(frame, text="QUIT", fg="red", command=quit) button.pack(side=tk.LEFT) slogan = tk.Button(frame, text="Hello", command=write_slogan) slogan.pack(side=tk.LEFT) root.mainloop() The result of the previous example looks like this: The following script shows an example, where a label is dynamically incremented by 1 until a stop button is pressed:
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to create a button input
r/learnpython on Reddit: How To Create a Button Input
October 1, 2024 -

I understand this is a beginner question but I am creating a desktop game because I really, REALLY, have nothing better to do. Here is the base code.

count = 1000
while True;
    print{f"Bottle(s) left: {count}"}
    if count > 0
        count -= 1
        time.sleep(1)
    else:
        count = 1000
        print("All bottles have been seized")
        time.sleep(5)
        break

So my guess is that might have to put that in {} brackets and create some sort of input, but I do not no. I also have a second piece which requires a different button input. All it needs to do is display the number going down on two different objects as two different buttons are pressed, those being the spacebar, and the D key.

๐ŸŒ
The Python Code
thepythoncode.com โ€บ article โ€บ make-a-button-using-pygame-in-python
How to Make Buttons in PyGame - The Python Code
Learn how to make buttons in PyGame that support pressed calling (multi pressing) and one-shot pressing in Python.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3184128 โ€บ can-you-make-a-button-in-python
Can You Make A Button In Python? | Sololearn: Learn to code for FREE!
Hey CgmaxjakeYT, there is a library called Tkinter that lets you create Graphical User Interfaces with Buttons and more. I recommend you read the documentation: https://docs.python.org/3/library/tkinter.html There are examples on how to import and use the library in your python project.
๐ŸŒ
Tech with Tim
techwithtim.net โ€บ tutorials โ€บ python-module-walk-throughs โ€บ kivy-tutorial โ€บ creating-buttons-triggering-events
Python Kivy Tutorial - Creating Buttons & Triggering Events
This kivy python tutorial will be covering creating buttons in kivy. It will show how to bind functions to button presses and how to access user input.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-make-a-button-in-Python-without-Tkinter
How to make a button in Python without Tkinter - Quora
Answer (1 of 3): You need some form of gui toolkit to produce buttons. They range from simplicity (zenity) to complex (gtk, Wx, qt and tinker). If all you need is a simple โ€˜goโ€™ button., look at zenity (and specifically pyZenity) - if you ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-tkinter-button
How To Create Buttons In Python With Tkinter?
March 19, 2025 - In this example, we create a window, define a style named โ€œTButtonโ€ with specific properties, create a button using ttk.Button the โ€œTButtonโ€ style applied, and pack the button in the window. You can further customize the button styles by modifying additional properties such as relief, width, height, and more. Read How to convert Python file to exe using Pyinstaller