The problem here is that you're only doing your mouseover test once, at the start of the function. If the mouse later moves into your rectangle, it won't matter, because you never do the test again.

What you want to do is move it into the event loop. One tricky bit in PyGame event loops is which code you want to run once per event (the inner for event inโ€ฆ loop), and which you only want to run once per batch (the outer while intro loop). Here, I'm going to assume you want to do this once per event. So:

def game_intro():
    intro = True

    # ... other setup stuff 

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

            mouse = pygame.mouse.get_pos()

            if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
                pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
            else:
                pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))

It looks like some of the other stuff you do only once also belongs inside the loop, so your game may still have some problems. But this should get you past the hurdle you're stuck on, and show you how to get started on those other problems.

Answer from abarnert on Stack Overflow
Top answer
1 of 3
2

The problem here is that you're only doing your mouseover test once, at the start of the function. If the mouse later moves into your rectangle, it won't matter, because you never do the test again.

What you want to do is move it into the event loop. One tricky bit in PyGame event loops is which code you want to run once per event (the inner for event inโ€ฆ loop), and which you only want to run once per batch (the outer while intro loop). Here, I'm going to assume you want to do this once per event. So:

def game_intro():
    intro = True

    # ... other setup stuff 

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

            mouse = pygame.mouse.get_pos()

            if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
                pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
            else:
                pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))

It looks like some of the other stuff you do only once also belongs inside the loop, so your game may still have some problems. But this should get you past the hurdle you're stuck on, and show you how to get started on those other problems.

2 of 3
1

Here is a button that should suit your needs:

class Button(object):
    global screen_width,screen_height,screen
    def __init__(self,x,y,width,height,text_color,background_color,text):
        self.rect=pygame.Rect(x,y,width,height)
        self.x=x
        self.y=y
        self.width=width
        self.height=height
        self.text=text
        self.text_color=text_color
        self.background_color=background_color

    def check(self):
        return self.rect.collidepoint(pygame.mouse.get_pos())

    def draw(self):
        pygame.draw.rect(screen, self.background_color,(self.rect),0)
        drawTextcenter(self.text,font,screen,self.x+self.width/2,self.y+self.height/2,self.text_color)  
        pygame.draw.rect(screen,self.text_color,self.rect,3)

Use the draw function to draw your button, and the check function to see if the button is being pressed.

Implemented into a main loop:

button=Button(x,y,width,height,text_color,background_color,text)
while not done:
    for event in pygame.event.get():
            if event.type==QUIT:
                terminate()
            elif event.type==pygame.MOUSEBUTTONDOWN:
                if button.check():
                       #what to do when button is pressed

    #fill screen with background
    screen.fill(background)

    button.draw()

    pygame.display.flip()
    clock.tick(fps)
๐ŸŒ
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 ...
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ how to create a button from an image without tkinter using zelle's graphics?
r/Python on Reddit: How to create a button from an image without tkinter using Zelle's graphics?
December 5, 2016 - --- If you have questions or are new to Python use r/LearnPython ... How can I make the mouse do something once it's clicking at a certain point in Zelle's graphics? What I am trying to do is make my stopwatch start when I click the "startbutton" image. However, I am obviously doing something wrong because my program either crashes or doesn't do anything. from graphics import * import time #from tkinter ...
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-28552.html
button command tkinter
May 17, 2021 - Hello Python Users: The following is a simple Python program. If you press the button with label red it prints 'red'. If you press the button with label blue it prints 'blue'. One thing that frustrates me is that I have to write a function for ev...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ python gui module for noob without tkinter?
r/learnprogramming on Reddit: Python GUI module for noob without tkinter?
May 13, 2023 -

Hi, I'm a noob of python want to make simple text editor. And I would rather use other module than using tkinter because it doesn't have so nice UI. What is your favourite module to reccommend to noobs?

๐ŸŒ
Quora
quora.com โ€บ Is-it-possible-to-make-a-GUI-in-Python-without-external-libraries
Is it possible to make a GUI in Python without external libraries? - Quora
Answer (1 of 3): That depends on what you mean by โ€œexternalโ€ and how much of a purist you are. Tcl/Tk is an external library although it comes as part of the standard python install(albeit there are potential issues with the std install of Tcl if you are on OSX like I am).
๐ŸŒ
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.

๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Button problem, got there eventually - Python Help - Discussions on Python.org
August 19, 2022 - I was learning how to use Tkinkter. I am incredibly new at this and I was trying to change the colour of a button. My code was: button = Button(box, text = "Enter", command = printbox, padx=10, pady=10, bg="blue", fg="โ€ฆ
Find elsewhere
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ tk_button.htm
Tkinter Button
The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the
๐ŸŒ
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 to fit your projects.
๐ŸŒ
GitHub
github.com โ€บ TomSchimansky โ€บ CustomTkinter
GitHub - TomSchimansky/CustomTkinter: A modern and customizable python UI-library based on Tkinter ยท GitHub
import customtkinter customtkinter.set_appearance_mode("System") # Modes: system (default), light, dark customtkinter.set_default_color_theme("blue") # Themes: blue (default), dark-blue, green app = customtkinter.CTk() # create CTk window like you do with the Tk window app.geometry("400x240") def button_function(): print("button pressed") # Use CTkButton instead of tkinter Button button = customtkinter.CTkButton(master=app, text="CTkButton", command=button_function) button.place(relx=0.5, rely=0.5, anchor=customtkinter.CENTER) app.mainloop()
Starred by 13.3K users
Forked by 1.2K users
Languages ย  Python
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-creating-a-button-in-tkinter
Python Tkinter - Create Button Widget - GeeksforGeeks
August 22, 2025 - Then, it creates a tkinter window (root) and a button within it, configured with various options like text, color, font, and behavior.
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()

๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python tkinter button as input
r/learnpython on Reddit: Python tkinter button as input
August 12, 2021 -

I have a question, is it possible to use a button as input, like normally when you want to give an input you use x = input() and then type it in console, but is it also possible to click a button that has a value for example 1 and then python reads that input as 1 and then x = 1.

๐ŸŒ
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 ...
๐ŸŒ
Python Basics
pythonbasics.org โ€บ home โ€บ tkinter โ€บ tkinter buttons (gui programming)
Tkinter buttons (GUI Programming) - pythonbasics.org
Buttons are standard widgets in a GUI. They come with the default Tkinter module and you can place them in your window. A Python function or method can be assoc