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 OverflowThe 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.
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)
Videos
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?
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)
breakSo 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.
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.
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()

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.
I'm trying to add a button to the main tk window that is running while it's running. So example run would be..
mainWindow opens up, you click "addButton". Another tkinter window opens up, you type in the name of the new button and then you click save and that window destroys, now I want the mainWindow to refresh and update with the new button. How could I go about accomplishing this?