[pygame] how do i create Buttons?

[pygame] how do i create Buttons?

Postby spamynator_1 » Wed Jul 31, 2013 5:21 pm

i have been working on a game, and i cant after hours of websearching and trying find a way to make an image clickable as a button, im starting to question weather it is even possible.

anybody know how to make a button?
Last edited by Yoriz on Wed Jul 31, 2013 5:44 pm, edited 1 time in total.
Reason: changed title
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: Buttons?

Postby metulburr » Wed Jul 31, 2013 5:25 pm

i have been working on a game, and i cant after hours of websearching and trying find a way to make an image clickable as a button, im starting to question weather it is even possible.

anybody know how to make a button?

every GUI framework can do that, but you didnt even mention what GUI you are using?
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2243
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Buttons?

Postby spamynator_1 » Wed Jul 31, 2013 5:30 pm

what do you mean, i am using python pygame, with idle.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: Buttons?

Postby metulburr » Wed Jul 31, 2013 5:35 pm

IDLE is the IDE you are using
pygame is the GUI

Anyways, in pygame, you have to create your own button framework. First try making a button using rectangles, button changing color when mouse over, click and and link it to some function (for example)
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2243
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Buttons?

Postby Yoriz » Wed Jul 31, 2013 5:39 pm

Google just found this link for me
Designing a Button UI Module for Pygame
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: [pygame] how do i create Buttons?

Postby spamynator_1 » Wed Jul 31, 2013 6:32 pm

That tells me how to make a button, but i want to make a .png work as a button, or atleast a way to make it so when i click on certain coords it does something.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: [pygame] how do i create Buttons?

Postby metulburr » Wed Jul 31, 2013 6:41 pm

you can either make a rect of the button or whatever your using, and check if the mouse is inside the rect while a mouse button press: (which is prolly what you should end up doing/ making a rect button with a rect)
http://www.pygame.org/docs/ref/rect.htm ... llidepoint

or you can check the actual X and Y coords of the mouse:
http://www.pygame.org/docs/ref/mouse.ht ... se.get_pos

here is a basic structure, not with an image, but you get the idea:
Code: Select all
import pygame
pygame.init()

class Button:
   def __init__(self, text):
      self.text = text
      self.is_hover = False
      self.default_color = (100,100,100)
      self.hover_color = (255,255,255)
      self.font_color = (0,0,0)
      self.obj = None
      
   def label(self):
      '''button label font'''
      font = pygame.font.Font(None, 20)
      return font.render(self.text, 1, self.font_color)
      
   def color(self):
      '''change color when hovering'''
      if self.is_hover:
         return self.hover_color
      else:
         return self.default_color
         
   def draw(self, screen, mouse, rectcoord, labelcoord):
      '''create rect obj, draw, and change color based on input'''
      self.obj  = pygame.draw.rect(screen, self.color(), rectcoord)
      screen.blit(self.label(), labelcoord)
      
      #change color if mouse over button
      self.check_hover(mouse)
      
   def check_hover(self, mouse):
      '''adjust is_hover value based on mouse over button - to change hover color'''
      if self.obj.collidepoint(mouse):
         self.is_hover = True
      else:
         self.is_hover = False
         
if __name__ == '__main__':

         
   btn = Button('Button 1')
   btn2 = Button('Button 2')
   btn3 = Button('Button 3')
   screen = pygame.display.set_mode((600,400))
   clock = pygame.time.Clock()

   run = True
   while run:
      screen.fill((0,0,0))
      mouse = pygame.mouse.get_pos()
      for event in pygame.event.get():
         if event.type == pygame.QUIT:
            run = False
         elif event.type == pygame.MOUSEBUTTONDOWN:
            if btn.obj.collidepoint(mouse):
               print('button 1 clicked')
            elif btn2.obj.collidepoint(mouse):
               print('button 2 clicked')
            elif btn3.obj.collidepoint(mouse):
               print('button 3 clicked')
      
      btn.draw(screen, mouse, (100,100,100,20), (125,103))
      btn2.draw(screen, mouse, (100,130,100,20), (125,133))
      btn3.draw(screen, mouse, (100,160,100,20), (125,163))
      #btn.check_hover(mouse)
      
      pygame.display.update()
      clock.tick(60)
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2243
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [pygame] how do i create Buttons?

Postby spamynator_1 » Wed Jul 31, 2013 7:27 pm

how could i use this to make an invisable box where if you click it it does a function? please label where the size and location perameters are.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: [pygame] how do i create Buttons?

Postby spamynator_1 » Wed Jul 31, 2013 9:35 pm

i need a script where i can select button size location and picture on the button, does such a script exist or do you have to use windows 95 style buttons for every game ever invented in pyhton?
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: [pygame] how do i create Buttons?

Postby metulburr » Wed Jul 31, 2013 10:44 pm

i need a script where i can select button size location and picture on the button, does such a script exist or do you have to use windows 95 style buttons for every game ever invented in pyhton?

such a script exists if you write it or pay someone to write it, because no one is going to do it for you.
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2243
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [pygame] how do i create Buttons?

Postby Mekire » Thu Aug 01, 2013 1:55 am

You will be able to find plenty of Pygame button modules around with a simple search... but if you don't know how they work they still won't help you. Anytime you want to see if the mouse is over a region of the screen, you will probably want to have a pygame.Rect and use the function pygame.Rect.collidepoint. This will be a fundamental concept for any game you make that requires user mouse interaction; not just buttons. You will need to understand how it works. In fact, using rects is so fundamental in Pygame that you won't be able to get much done without a firm grasp of their purpose and usage.

Pygame is not a game engine; Pygame is not a GUI toolkit; Pygame is an API. This means that you will need to be able to program to make it work. It doesn't have a built in library of GUI widgets. You will either need to write your own, or find a library someone has written for that purpose. Your first goal should be understanding how it works, and writing a basic button interface is a great exercise.

-Mek
New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Mekire
 
Posts: 1710
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona


Return to Game Development

Who is online

Users browsing this forum: No registered users and 4 guests