tommycheok
Posts: 21
Joined: Tue Feb 26, 2013 12:49 pm

How to create interface with python?

Sat Mar 09, 2013 11:57 am

I need to run a DC motor in order of clockwise and counter clockwise with speed, the code is running fine in LXTerminal. Now the problem is how do I create a interface which include" clockwise, counter clockwise with speed" which will be more user friendly? any link? or information? Thanks :?

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: How to create interface with python?

Sat Mar 09, 2013 12:12 pm

Tkinter is a lightweight gui using python. Its already installed in the Pi

http://effbot.org/tkinterbook/
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

tommycheok
Posts: 21
Joined: Tue Feb 26, 2013 12:49 pm

Re: How to create interface with python?

Sat Mar 09, 2013 4:12 pm

when i try to compile the tkinter which ony simple hello world. it is successfully compiled using ssh but not the LXTerminal in rpi. Why? :?:

Error come out as shown:
Client is not authorized to connect to ServerTraceback (most recent call last):
File "tk.py", line 4, in <module>
root = Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1712, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":1.0"

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: How to create interface with python?

Sat Mar 09, 2013 5:21 pm

Weird. i would expect the opposite

I think Tkinter will not work from SSH but should work from LXTerminal on the Pi

By compile i assume you mean run as in python myprog.py
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

tommycheok
Posts: 21
Joined: Tue Feb 26, 2013 12:49 pm

Re: How to create interface with python?

Sun Mar 10, 2013 5:20 am

KenT wrote:Weird. i would expect the opposite

I think Tkinter will not work from SSH but should work from LXTerminal on the Pi

By compile i assume you mean run as in python myprog.py
yeah, i run in ssh : pi@raspberrypi ~ $ sudo python tk.py... it successful run and prompt out a Xming server in window but not in raspi..weird. any clue? =(

Btw, the os i using is occidentalis V0.2

tommycheok
Posts: 21
Joined: Tue Feb 26, 2013 12:49 pm

Re: How to create interface with python?

Sun Mar 10, 2013 6:34 am

however..i had successfully run in IDLE @@

DBryant
Posts: 281
Joined: Sat Feb 02, 2013 12:41 pm
Location: Berkshire, UK

Re: How to create interface with python?

Sun Mar 10, 2013 7:59 am

You've not given too many details on the way you are invoking your Python app.

Via ssh you are, by some means, enabling X11 forwarding (ssh -X ...) in which graphic traffic comes back to the X server on the system you invoked the ssh command. The mechanics of ssh ensures that the correct DISPLAY is forwarded to the remote app, so it knows where to send its output. It usually manages to sort out authentication, although you may see some warning messages; we generally ignore them because the application runs!

On an LXterminal then the (default) X server is the created during the startx process; same app but a completely different graphical display. And the authentication is not complete in this basic environment, but apparently it is in IDLE. In the LXterminal then run

Code: Select all

echo $DISPLAY
:0.0
which reports the graphical display that will be used by default. In this case display 0, screen 0 on the localhost. If is does't report anything then

Code: Select all

export DISPLAY=:0.0
where were guessing there's only a single display with the usual numbers.
Then extend the authentication with

Code: Select all

xhost +
access control disabled, clients can connect from any host
this open up access to the local X server as reported by the command. Now try to run your Python application in the LXterminal.

If it works the appropriate commands can be inserted in the shell startup file (for bash this is .bashrc). You might want to read man xhost http://unixhelp.ed.ac.uk/CGI/man-cgi?xhost+1 and man xauth http://www.x.org/archive/X11R6.8.1/doc/xauth.1.html since there are security issues with opening up access for X11 traffic. Not a serious issue on a home network behind a router/firewall, but something to be aware of.

Hope this helps

tommycheok
Posts: 21
Joined: Tue Feb 26, 2013 12:49 pm

Re: How to create interface with python?

Sun Mar 10, 2013 8:53 am

DBryant wrote:You've not given too many details on the way you are invoking your Python app.

Via ssh you are, by some means, enabling X11 forwarding (ssh -X ...) in which graphic traffic comes back to the X server on the system you invoked the ssh command. The mechanics of ssh ensures that the correct DISPLAY is forwarded to the remote app, so it knows where to send its output. It usually manages to sort out authentication, although you may see some warning messages; we generally ignore them because the application runs!

On an LXterminal then the (default) X server is the created during the startx process; same app but a completely different graphical display. And the authentication is not complete in this basic environment, but apparently it is in IDLE. In the LXterminal then run

Code: Select all

echo $DISPLAY
:0.0
which reports the graphical display that will be used by default. In this case display 0, screen 0 on the localhost. If is does't report anything then

Code: Select all

export DISPLAY=:0.0
where were guessing there's only a single display with the usual numbers.
Then extend the authentication with

Code: Select all

xhost +
access control disabled, clients can connect from any host
this open up access to the local X server as reported by the command. Now try to run your Python application in the LXterminal.

If it works the appropriate commands can be inserted in the shell startup file (for bash this is .bashrc). You might want to read man xhost http://unixhelp.ed.ac.uk/CGI/man-cgi?xhost+1 and man xauth http://www.x.org/archive/X11R6.8.1/doc/xauth.1.html since there are security issues with opening up access for X11 traffic. Not a serious issue on a home network behind a router/firewall, but something to be aware of.

Hope this helps
OMG. It solve the problem. Thanks alot!!! =)

Return to “Python”