Python and Pygame inspiration and best practice - python

Sorry for not being very specific in this question, but I don't know where else to ask.
I have a Raspberry Pi which I will try to use in a car computer project. I have got the tip to use Pygame to write the interface.
I'm not really familiar to python or pygame but i have quite experience in PHP, HTML and C# and VB against winforms.
In this case I will have a menu to the right with lets say 5 "tabs". How would I think when doing this? Should I think PHP/HTML and have a file for every tab page and here then load header and background and stuff, and some how link to every *.py file (if that's even possible)? Should i think more of JS and ajax to use a surface (a "div") to display my pages in, depending on the selected menu?
I've spent an evening playing around and what I suppose i have to do is something like this:
//Set up the layout
//Add a surface for Tab 1 (tab1Layout.py)
//Add a surface for Tab 2 (tab2Layout.py) - set invisible
//Add a surface for Tab 3 (tab3Layout.py) - set invisible
//Add a surface for Tab 4 (tab4Layout.py) - set invisible
//Add a surface for Tab 5 (tab5Layout.py) - set invisible
//Add the menu to the right (menu.py)
//Start the pygame loop
//Listen for events in the menu (menuEvents.py)
//Listen for events in tab 1 (tab1Events.py)
//Listen for events in tab 2 (tab2Events.py)
//Listen for events in tab 3 (tab3Events.py)
//Listen for events in tab 4 (tab4Events.py)
//Listen for events in tab 5 (tab5Events.py)
Am i on the right track here?
Do you guys know any good tutorials or other libraries for doing this kind of stuff? I do not wanna load X or something like that.
Any good tips of tutorials or know any similar projects where I can study the code for this?

So, slightly hard to know exactly what you want. However I would like to champion simplicity here, if you make yourself this many files you'll just confused, and have a lot of redundant code.
From my person game making perspective, my screens are all methods of a single class. There are a few instance variables like the screen, but each method contain the main pygame loop, i.e.
while True:
for event in pygame.event.get():
#whatever
pass
and on button press one can move on to a separate method in your class.
I would recommend creating either a class for each tab button or a class for the whole set of tab buttons, this can be updated, drawn to screen and used in all separate methods for each of your tabs
The way this varies from things like php & html is the lack of predefined structure, as you will have to make all of this from scratch. I have a few code snippets available on my website that you can use for making buttons etc if that would help. Here is a link to it.
Also, If you choose the seperate file method, python's import works very intutively, you could import a class (say Layout3) from your file tabLayout3.py simply using:
from tabLayout3 import Layout3
(provided the path of tabLayout3.py is in my python path)

Related

Python wx.notebook tab's controls - reading one from another

Quick question: I have a python application with 3 tabs. Is it possible to read the state of a control on tab 2 from tab 1? That is, on tab 1 I have a run button. When clicking this I want to determine which checkboxes are checked on tab 2.
Should be quite simple, but I can't seem to suss it!
Many thanks
It should be possible, yes. All the controls are usually private members of the wxFrame class. I don't really understand what is the problem.

How to record mouse clicks on mac to be replayed by an apple script?

I am writing a script to automate repeatedly registering new users for a website (not boosting metrics, not what you think!). I can boil down the process of registering to a series of mouse clicks and typing. I know there are some macro recorders that will let me record how I use the GUI and repeat it, but I need to type something a little different every time - however the mouse clicks are always the same.
Imagining script to look kindof like:
username = "something"
for i in range(0,100):
playback recorded series of mouse clicks A
type username + str(i)
type some other stuff
playback recorded series of mouse clicks B
But I can't find a good tool to record a series of mouse clicks so that I can play them back from either an applescript or a python script.
I found a useful script (http://www.bluem.net/en/mac/cliclick/) I can call that takes in 2 coordinates and clicks. So even if I could find a tool to record the coordinates of mouseclicks that would work!
You can also run JavaScripts in browsers with something like tell application "Safari" to tell document 1 to do JavaScript or tell application "Google Chrome" to tell tab 1 of window 1 to execute javascript.
tell application "Safari" to tell document 1
do JavaScript "document.querySelectorAll('.mainnavs ul li:nth-child(2) a')[0].click()"
delay 1
repeat until do JavaScript "document.readyState" is "complete"
delay 0.1
end repeat
do JavaScript "document.getElementById('tagfilter').value = 'aa'"
end tell
System Events has commands for simulating clicks and keystrokes:
delay 1
tell application "System Events" to tell process "Safari"
click at {76, 117}
repeat with i from 1 to 3
keystroke "aa" & i & return
end repeat
keystroke "a" using command down
end tell
You can see the positions of UI elements from Accessibility Inspector.app. It is part of Xcode, but it can also be downloaded from developer.apple.com/downloads.
If you want to get the screen coordinates of where you want to perform mouse clicks, you can use a free tool I made called MouseTools found here. I made it for just this purpose. You'll want to do something like I show in the example #1 applescript near the bottom of the page. Just move your mouse to a position on the screen and run the applescript to get the coordinates. Note: you'll want to move your mouse to a location and press command-r (while the applescript is frontmost) to run the script in AppleScript Editor.
In addition, I found that clicking at screen coordinates, as Lauri Ranta showed in her applescript using System Events, often does not reliably work. System Events is good at clicking in the Finder however it often has trouble clicking in the window of an application like Safari. If you find that problem too then you can also use the MouseTools to perform the mouse clicks. Again, this is why I created the program and there are examples of how to do this on the web page.
I hope it helps. Good luck.
How about Carsten Blum's Clicclick?
His link
It is a shell app which will emulate mouse clicks at arbitrary screen coordinates. This will work with applescript.
Hopefully you are doing test automation!
Have you looked into using something like Selenium WebDriver instead?

Button Transition

I am programming wxPython with GUI aid from pythonCard. The resource editor has made short work of my display so far.
The GUI has static boxes "cards" with information that the program outputs. I would like to transition to a different page on button click. So for example, clicking on ok should take me to page 2 (separate resource file).The complexity is I don't want to open up a new window for each click as I have 20 buttons.
I have 10 cards with same properties so on click i only want to change the particular and still be on main page however go to page two for particular card. Visually think of windows 8 tile and on click it flips to reveal more information.
Is this possible in wxPython? If so, how could i go about it?
It sounds to me like you want a Notebook or similar "book" control. The wxPython demo details the various book controls that wx supports. Or you might want to just swap panels, which is pretty easy to do as well. Here's a tutorial I wrote on the subject a while back:
http://www.blog.pythonlibrary.org/2010/06/16/wxpython-how-to-switch-between-panels/
And here are a couple links on notebooks and similar controls:
http://wxpython.org/docs/api/wx.Notebook-class.html
http://wiki.wxpython.org/Simple%20wx.Notebook%20Example
http://www.blog.pythonlibrary.org/2012/07/18/wxpython-how-to-programmatically-change-wx-notebook-pages/

Interactive movie / video processing with PyQT / OpenCV

I am starting out on a project in which I need to build a customized annotation tool for movies and video. Some person (not technically minded) will need to pop open a GUI that I create, open either a video file or a directory of frames that result from chopping up a video file, and then use a window (much like QuickTime or VLC player, etc., i.e. a video window with a simple slider bar allowing the user to move back and forth at will). In this window, the user will be able to click on interesting points, give them semantic labels and meta-data (such as whether or not the point is occluded by something else in the picture), and then basically "press go" and start a tracker. The tracker will follow the points, frame by frame and the user can press the space bar or something to move forward and backward. The idea is to allow the human to intervene any time the tracker gets confused, but hopefully the tracker works well enough that humans don't have to hand-label every frame in an entire multi-thousand frame video sequence.
I am planning to do this all in Python, (a) because it is the language I know best for non-trivial programming, (b) I have easy access to both OpenCV Python (for image processing algorithms) and PyQt which seems to have a powerful enough GUI toolbox for what I want to do and (c) some other aspects of this same project are being developed by other programmers to work in Python and with MySQL databases. Python just seems like the natural choice to streamline it all together.
I am experienced using computer vision algorithms for the tracking, and I am reasonably sure that I can figure out simple PyQt GUI devices for making points clickable, buttons, entering simple text data, etc. However, the part I am having trouble understanding is how to actually construct my own video window with a slider bar that either moves ahead according to a frame's number or else is actually manipulating a video file. Can I leverage other movie players like VLC from within PyQt when programming in Python? Any suggestions or links that describe similar movie/video editing GUIs and how to develop them at home would be greatly appreciated.
Qt(PyQt) has a good multimedia support via Phonon module. You can easily use that module to achieve Video window, it can provide an easy-to-use video player and you can get playing position etc.

Can you auto hide frames/dialogs using wxPython?

I would like to create an application that has 3-4 frames (or windows) where each frame is attached/positioned to a side of the screen (like a task bar). When a frame is inactive I would like it to auto hide (just like the Windows task bar does; or the dock in OSX). When I move my mouse pointer to the position on the edge of the screen where the frame is hidden, I would like it to come back into focus.
The application is written in Python (using wxPython for the basic GUI aspects). Does anyone know how to do this in Python? I'm guessing it's probably OS dependent? If so, I'd like to focus on Windows first.
I don't do GUI programming very often so my apologies if this makes no sense at all.
As far as I know, there's nothing built in for this.
When the window is hidden, do you want it completely invisible or can a border of a few pixels be showing? That would be an easy way to get a mouse hover event. Otherwise you might have to use something like pyHook to get system-wide mouse events to know when to expand your window.
The events EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW might also be useful here to know when the user has entered/left the window so you can expand/collapse it.
Expanding/collapsing can just be done by showing/hiding windows or resizing them. Standard window functions, nothing fancy.
By the way, you might want to use wx.ClientDisplayRect to figure out where to position your window. That will give you a rectangle of the desktop that does NOT include the task bar or any other toolbars the user has, assuming you want to avoid overlapping with those things.
Personally, I would combine the EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW that FogleBird mentioned with a wx.Timer. Then whenever it the frame or dialog is inactive for x seconds, you would just call its Hide() method.
I think you could easily just make a window that is the same size as the desktop then do some while looping for an inactivity variable based on mouse position, then thread off a timer for loop for the 4 inactivity variables. I'd personally design it so that when they reach 0 from 15, they change size and position to become tabular and create a button on them to reactivate. lots of technical work on this one, but easily done if you figure it out

Categories