User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Python GUI?

Wed Apr 01, 2015 4:32 pm

Hi everyone, it's me again.

I was wondering if anyone could help me (or preferably teach me how to) build a GUI interface to a python program. I only need a few items, like buttons that execute code, and a slider (0 to 100) that executes code for every integer step it is moved. I also need some indicators, basically, if in1 == 1: {turn on virtual LED (as well as switch a hardware relay, but I can do that on my own)}. I'd also like to know if I could put this in a web interface, so that I could use my iPhone/Mac/PC/Android/etc to control from around the house (or the world if I set up port forwards :geek:).

If anyone asks why, I'm building a 400 Watt software-defined audio amp, network enabled, onboard media centre, etc, using the Pi2.
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Python GUI?

Wed Apr 01, 2015 4:41 pm

There are plenty of tutorials on the two that I would recommend (pygame & tkinter).

Both work well and there are plenty of resources about them.
There are 10 types of people: those who understand binary and those who don't.

User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Re: Python GUI?

Wed Apr 01, 2015 5:06 pm

I've tried Tkinter, but the button commands seem to execute on program runtime, not button click. Pygame sound like overkill.
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

riklaunim
Posts: 265
Joined: Tue Apr 22, 2014 7:34 pm

Re: Python GUI?

Wed Apr 01, 2015 6:24 pm

If it executes on runtime then something is wrong with the code. I use PyQt for GUI apps. Very high quality, good documentation and so on. If you want a web based GUI then the libraries and design will be quite different from a singal-slot driven GUI application. WeIO board uses a Tornado web server running on the board which offer GUI-pages with websockets for efficient handling of events (from server / to server).

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

Re: Python GUI?

Wed Apr 01, 2015 7:04 pm

Tkinter is pretty easy. Button presses work when they are pressed. I posted some example code at the below link which used multiple buttons.

If you need any help, post your code and I'll help you debug it.

http://www.raspberrypi.org/forums/viewt ... 72#p712472

This code used a few different widgets to control gpio http://www.raspberrypi.org/forums/viewt ... 32&t=41639
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

TrevorAppleton
Posts: 74
Joined: Wed May 30, 2012 7:26 pm
Contact: Website

Re: Python GUI?

Thu Apr 02, 2015 7:03 pm

I have created a few tutorials in the past on how to create a GUI with Python and Tkinter.

This one creates a game of Rock, Paper, Scissors!

http://trevorappleton.blogspot.co.uk/20 ... e-gui.html

Let me know if you have any questions.
Check out my blog post for Raspberry Pi and Python tutorials.

http://trevorappleton.blogspot.co.uk/

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Python GUI?

Fri Apr 03, 2015 8:44 am

This is yet another attempt to give some starting point code for a GUI. It has a menu which you can delete if you do not need it and one button. Clearly you can add more widgets.

http://www.raspberrypi.org/forums/viewt ... 2&t=105749

Roger Woollett

User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Re: Python GUI?

Mon Apr 06, 2015 5:00 pm

OK, I'll see if I can find my code (I may have trashed it out of frustration) and I'll post it when I can.
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Re: Python GUI?

Mon Apr 06, 2015 6:09 pm

I have attached a compression of the main folder for this project. Let me know what's wrong...
Attachments
KV4LQ.zip
(8.13 KiB) Downloaded 54 times
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Python GUI?

Tue Apr 07, 2015 6:26 am

Hi KV4LQ,

Have you seen this GUI? https://github.com/dddomodossola/gui

It allows to build web interfaces in an easy way, without writing HTML nor Javascript. Only Python code, and your graphic interface can be accessed locally and remotly.

User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Re: Python GUI?

Tue Apr 07, 2015 3:24 pm

Does the client (when coming over http connection) need python as well?
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

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

Re: Python GUI?

Tue Apr 07, 2015 4:04 pm

KV4LQ wrote:Does the client (when coming over http connection) need python as well?
With what dddomodossola is proposing: no. Any web browser enabled device should be able to access it.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Python GUI?

Tue Apr 07, 2015 4:16 pm

Scotty101 is right. The simplest way to try it is to run the simple_app.py. It shows a button and a onclick event.

User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Re: Python GUI?

Tue Apr 07, 2015 9:12 pm

OK thanks guys! I will let you know how things work out!

As a side question, what did you think of my code? Was it wrong coding, wrong libs, or just plain wrong?
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Re: Python GUI?

Tue Apr 07, 2015 9:14 pm

Oh, also: side this is a GUI interface, how difficult would it be to use with a touchscreen, say for instance, an 8" attached to the Pi? (Of course, I don't know how to hook up an 8" touchscreen for the Pi, if there is such a thing)
EDIT: I did find a 7" display on amazon, but I didn't like the $60 price tag. Anybody out there who knows of something wayyy cheaper, please let me know. I'd love to use a salvaged item. The touch is optional: just a screen would work (but be less functional obviously)
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Python GUI?

Wed Apr 08, 2015 6:29 am

As a side question, what did you think of my code? Was it wrong coding, wrong libs, or just plain wrong?
I think that your code is complicated by the intensive use of "os.system" calls. You do not need this.
The right way is to write functions, and not single executable modules. Also you have to remove the "while 1" in each module... You do not need this intensive "multitasking" execution.
Let's restart the project, step by step. Try with this code:

Code: Select all

import RPi.GPIO as GPIO
import gui #github.com/dddomodossola/gui
from gui import *

# Set input and output pin designations
input_pin=8
output_pin=7

class App( BaseApp ):
	def __init__( self, *args ):
		super( App, self ).__init__( *args )

	def main( self ):
		#the arguments are	width - height - layoutOrientationOrizontal
		wid = gui.widget( 100, 60, False )
		
		self.lbl = gui.labelWidget( 100, 30, "Hello world!" )
		self.bt = gui.buttonWidget( 100, 30, "Press me!" )

		#setting the listener for the onclick event of the button
		self.bt.setOnClickListener( self, "onButtonPressed" )
			
		#appending a widget to another, the first argument is a string key
		wid.append( "1", self.lbl )
		wid.append( "2", self.bt )
		
		self.output_pin_value = False
		
		#returning the root widget
		return wid
	
	#listener function
	def onButtonPressed( self ):
		self.output_pin_value = not self.output_pin_value
		if self.output_pin_value:
			GPIO.output(output_pin, 1)
		else:
			GPIO.output(output_pin, 0)
		
		self.lbl.setText( "Output pin value: " + str( self.output_pin_value ) )
		
#starts the webserver	
start( App )
In the gui you will see a button. When you will press it, you will see the output_pin switching between 1 and 0.
I didn't tested this code, in particular for the GPIO. Let me know the result of the execution.

Furthermore, do you really need to use bash scripts? Try to convert that code in python.

User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Re: Python GUI?

Sat Apr 11, 2015 7:37 pm

Well, your code is all fine and dandy, but I need a lot more than what you coded. I'm using the Pi to control an audio amp, including PWM for volume control (controls gain on an LM386 preamp). I need buttons to control relays essentially, as well as a slider to adjust the duty cycle on one pin. I'm also using hardware controls via GPIO, which the Pi needs to monitor and respond with the same actions as via the GUI. Does this gui lib have a slider, or will I have to improvise? Also, the values could be changing while the page is up, meaning a user would need to refresh to get updates.
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Python GUI?

Mon Apr 13, 2015 6:46 am

Does this gui lib have a slider, or will I have to improvise? Also, the values could be changing while the page is up, meaning a user would need to refresh to get updates.
Now I added new widgets, and a sliderWidget just for you. ;-)
The refresh is not a problem, You do not need to refresh manually the page. You only need to call the function 'setUpdateTimer' for the widget of which you would like to get auto-updated content.

A complete example is available in the library, I suggest you to start the widgets_overview_app.py.

Let me know if you encounter difficulties.

User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Re: Python GUI?

Mon Apr 13, 2015 2:33 pm

Will do. Thanks!
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Python GUI?

Thu Jun 25, 2015 6:09 am

Update:
New widget have been added to the GUI library.
Improved stability.
Improved speed adopting websockets.

gkreidl
Posts: 6095
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Python GUI?

Thu Jun 25, 2015 6:33 am

dddomodossola wrote:Update:
New widget have been added to the GUI library.
Improved stability.
Improved speed adopting websockets.
You should create your own project thread and update the first post each time an update becomes available (and add an update message at the bottom so people using it get a message).
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

User avatar
KV4LQ
Posts: 26
Joined: Thu Feb 12, 2015 4:32 pm
Location: Alabaster, AL, USA

Re: Python GUI?

Sat Jun 27, 2015 1:35 am

gkreidl wrote:
dddomodossola wrote:Update:
New widget have been added to the GUI library.
Improved stability.
Improved speed adopting websockets.
You should create your own project thread and update the first post each time an update becomes available (and add an update message at the bottom so people using it get a message).

I've kinda put this project on hold (at least software wise) since I'm having some hardware issues that I'm trying to work out. I'll do your suggestion, but I need to make sure I can make my concepts work.
"What if soy milk is just regular milk introducing itself in spanish?"
Daniel Smith, KV4LQ

Return to “Python”