leejohnson
Posts: 43
Joined: Sun Sep 28, 2014 7:01 pm

help on a couple of pythons question please :)

Wed Dec 03, 2014 2:21 pm

Hey only me again i have two questions for my gui im working on.

1. i have created a settings page and want to have a slidder bar on the page to control the dimmer on the screen (like a phone or sat nav does) at to save the light setting for all screens.

2. im in need of a countdown when a button is pressed (take photo) i want print on screen a 5 second countdown (using my font) and then Smile!!! then the pi camera take the photo.

i have gone through varies website but havent found something quite right , is this possible, as always thank you in advance

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: help on a couple of pythons question please :)

Wed Dec 03, 2014 5:11 pm

What are you using to create your GUI?

Tkinter?
Pygame?
wxWidgets?
Kivy?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: help on a couple of pythons question please :)

Wed Dec 03, 2014 5:37 pm

Maybe something to look at?
http://www.raspberrypi.org/forums/viewt ... 38&t=75899

Laurens

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: help on a couple of pythons question please :)

Thu Dec 04, 2014 10:04 am

I've wondered how to get pygame to read the mouse buttons as you move the mouse, so I did some investigation. Here's one solution to your slider question: The slider (red bar) will move with the mouse when you press the left mouse button. The value of the position will be x in the program.

Gordon77

Code: Select all

#!/usr/bin/python
import os
import pygame, sys
from pygame.locals import *

# set window size
width = 640
height = 100

# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
redColor = pygame.Color(255,0,0)
blackColor = pygame.Color(0,0,0)

#starting position
x = 100
pygame.draw.rect(windowSurfaceObj,redColor,Rect(x,5,10,90))
pygame.display.update(pygame.Rect(0,0,width,height))

s = 0
while s == 0:
    button = pygame.mouse.get_pressed()
    if button[0] != 0:
       pos = pygame.mouse.get_pos()
       x = pos[0]
       y = pos[1]
       a = x - 5
       if a < 0:
          a = 0
       pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
       #pygame.display.update(pygame.Rect(0,0,width,height))
       pygame.draw.rect(windowSurfaceObj,redColor,Rect(a,5,10,90))
       pygame.display.update(pygame.Rect(0,0,width,height))

       
   # check for ESC key pressed, or pygame window closed, to quit
    for event in pygame.event.get():
       if event.type == QUIT:
          pygame.quit()
          sys.exit()
       elif event.type == KEYDOWN:
          if event.key == K_ESCAPE:
             pygame.quit()
             sys.exit()


leejohnson
Posts: 43
Joined: Sun Sep 28, 2014 7:01 pm

Re: help on a couple of pythons question please :)

Thu Dec 04, 2014 11:11 am

scotty sorry i thought i had put it in.

it is in pygame

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: help on a couple of pythons question please :)

Thu Dec 04, 2014 11:38 am

Heres a version using images, save the attached inamges in home/pi

Gordon77

Code: Select all

#!/usr/bin/python
import os
import pygame, sys
from pygame.locals import *

# set window size
width = 256
height = 28

# initilaise pygame
pygame.init()
screen = pygame.display.set_mode((width,height),1,16)
slider= pygame.image.load('slider.jpg')
background= pygame.image.load('slider_back.jpg')

#starting position
x = 100
screen.blit(background,(0,0))
screen.blit(slider,(x,5))
pygame.display.update(pygame.Rect(0,0,width,height))

s = 0
while s == 0:
    button = pygame.mouse.get_pressed()
    if button[0] != 0:
       pos = pygame.mouse.get_pos()
       x = pos[0]
       y = pos[1]
       if x < 5:
          x = 5
       if x > 240:
          x = 240
       screen.blit(background,(0,0))
       screen.blit(slider,(x,5))
       pygame.display.update(pygame.Rect(0,0,width,height))

       
   # check for ESC key pressed, or pygame window closed, to quit
    for event in pygame.event.get():
       if event.type == QUIT:
          pygame.quit()
          sys.exit()
       elif event.type == KEYDOWN:
          if event.key == K_ESCAPE:
             pygame.quit()
             sys.exit()
Attachments
slider_back.jpg
slider_back.jpg (1.83 KiB) Viewed 1517 times
slider.jpg
slider.jpg (827 Bytes) Viewed 1517 times
Last edited by gordon77 on Fri Dec 05, 2014 9:26 am, edited 2 times in total.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: help on a couple of pythons question please :)

Thu Dec 04, 2014 12:27 pm

I've found PGU very useful for creating GUIs https://code.google.com/p/pgu/

I worked with a small team to develop an air traffic control game that used pygame and pgu. It had some sliders and buttons. Link is https://code.google.com/p/python-air-traffic-control/
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

leejohnson
Posts: 43
Joined: Sun Sep 28, 2014 7:01 pm

Re: help on a couple of pythons question please :)

Thu Dec 04, 2014 2:32 pm

wow i like that, thank you very much guys on your help with the slidder.

btw is there an easy way of making a button function. for example

screenmode 1 has 2 buttons say ( yes) (no)

if (yes) pressed go to screenmode 2
if (no) pressed go to screenmode 3

thats what i want to achieve but havent dived into code yet to work it out.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: help on a couple of pythons question please :)

Fri Dec 05, 2014 8:41 am

Lee,
You asked about controlling the brightness with the slider, here's a version that does that by varying the colours.

Gordon77

Code: Select all

#!/usr/bin/python
import os
import pygame, sys
from pygame.locals import *

# set window size
width = 255
height = 100

# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)


#starting position
x = 128
redColor = pygame.Color(x,0,0)
whiteColor = pygame.Color(x,x,x)
pygame.draw.rect(windowSurfaceObj,whiteColor,Rect(0,0,width,height))
pygame.draw.rect(windowSurfaceObj,redColor,Rect(x,5,10,90))
pygame.display.update(pygame.Rect(0,0,width,height))

s = 0
while s == 0:
    button = pygame.mouse.get_pressed()
    if button[0] != 0:
       pos = pygame.mouse.get_pos()
       x = pos[0]
       redColor = pygame.Color(x,0,0)
       whiteColor = pygame.Color(x,x,x)
       y = pos[1]
       a = x - 5
       if a < 0:
          a = 0
       pygame.draw.rect(windowSurfaceObj,whiteColor,Rect(0,0,width,height))
       pygame.draw.rect(windowSurfaceObj,redColor,Rect(a,5,10,90))
       pygame.display.update(pygame.Rect(0,0,width,height))

       
   # check for ESC key pressed, or pygame window closed, to quit
    for event in pygame.event.get():
       if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
          pygame.quit()
          sys.exit()

Return to “Python”