Beginner python learner here. I have a question that I have tried to Google but I just can't come up with the proper way to ask in just a few words (partly because I don't know the right terminology.)
How do I get python to detect other widgets? For example, if I wanted a script to check and see when I click my mouse if that click put focus on an entry widget on a (for example) website. I've been trying to get it to work in Tkinter and I can't figure out even where to begin.
I've seen this:
focus_displayof(self)
Return the widget which has currently the focus on the
display where this widget is located.
But the return value for that function seems to be some ambiguous long number I can't decipher, plus it only works in its own application.
Any direction would be much appreciated. :)
Do you mean inside your own GUI code, or some other application's/website's?
Sounds like you're looking for a GUI driver, or GUI test/automation driver. There are tons of these, some great, some awful, many abandoned. If you tell us more about what you want that will help narrow down the choices.
Is this for testing, or automation, or are you going to drive the mouse and button yourself and just want something to observe what is going on under the hood in the GUI?
>How do I get Python to detect other widgets?
On a machine, or in a browser? If in a machine, which platform: Linux/Windows (which)/Mac?
If in a browser, which browser (and major version)?
> But the return value for that function seems to be some ambiguous long number I can't decipher
Using longs as resource handles is par for the course, although good GUI drivers also work with string/regex matching on window and button names.
> plus it only works in its own application.
What do you mean, and what are you expecting it to return you? You should be able to look up that GUI object and access its title. Look for a GUI driver that works with window and button names.
Here is one list, read it through and see what sounds useful. I have used AutoIt under Win32, it's great, widely-used and actively-maintained; it can be called from Python (via subprocess).
Here are comparisons by the author of PyWinAuto on his and similar tools. Give a read to his criticisms of its structure from 2010. If none of these is what you want, at least you now have the vocabulary to tell us what would be...
Related
I am trying to code a message box that just pops up and says something and has a single button to close it however as I only have a small amount of text in the box the icon/image to the left of it is huge and looks bad. I want to know how to remove it. I am also having trouble making custom message boxes. Tutorials say that this is how you make a custom message box:
box = QMessageBox()
box.setText('text')
box.addButton(QPushButton('Close', self))
box.exec_()
However this just closes my program and returns a 1. My current code uses the about method of QMessageBox():
box = QMessageBox().about(self, 'About', 'This is a test Program')
However this has that large icon in the text window and I can't seem to do anything else to the box as it just stops the program and returns 1 again
I am in desperate need of some decent PyQt documentation. I can't seem to find documentation on much at all unless it is in C++. For instance I cannot seem to find any information of options other than question and about for QmessageBox. So if someone could also show me where some proper documentation lives it would prevent me asking too many questions here
Rather than PyQt documentation, it is better to directly use Qt documentation. You only need to switch your language mindset from Python to C++, there and back. It is not that difficult. :) See e.g. http://doc.qt.io/qt-4.8/qmessagebox.html#addButton or http://doc.qt.io/qt-4.8/qmessagebox.html#about I think this is very detailed documentation, unrivaled by most other frameworks.
Note that there are three overrides of addButton(). From the documentation it seems that you either need to pass two arguments to box.addButton(QPushButton('Close', self), QMessageBox.RejectRole) (you forgot the role!) or better, you use the override which uses standard buttons, then you only pass one argument: box.addButton(QMessageBox.Close).
And one more tip for you: I also find it easier to debug my program with PySide than PyQt because unlike PyQt, PySide catches the exception, prints that to console and keeps running. While PyQt usually just silently crashes leaving you clueless. Most of the time, I am using shims Qt.py https://pypi.python.org/pypi/Qt.py/0.6.9 or qtpy https://pypi.python.org/pypi/QtPy to be able to switch from PyQt to PySide on the fly. It also allows switching between Qt4 and Qt5 bindings easily.
Is there a way to create menu's such as the one provided in WordPad on Windows 7 with Tkinter?
I haven't found anything like it, and it would improve the looks on my scripts immensely.
What you are describing is what Microsoft calls a ribbon. No, there is nothing built-in to tkinter to provide such a menu. Most of the building blocks are there to emulate it (notebooks, buttons, dropdowns, etc), but you'll have to do a fair amount of work to get it to work exactly like a ribbon.
The consensus on ribbons seem to be that they only aid usability when you have a very large amount of controls that you need to present to the user. That is why you see such ribbons on tools like Word and Excel, but not so much on browsers, notepad, etc. I would advise that unless your app needs such a rich toolbar, adding one won't actually improve the usability of your application.
I created an complete logger-type program, that logs the certain data from the internet sources. It's GUI I coded in wx.python, now I want to daemonize it (if it is the right term). The program needs to run in background and user has to have option to call/open GUI when he pleases. How can I achieve this with wx.python?
I wouldn't really "daemonize" it per se. Instead, I would just put it in the system tray...at least, that's what I would do on Windows. I assume you can do something similar on the other OSes. Basically you want to bind the frame to wx.EVT_ICONIZE and in that method, you hide it. Then when the user double-clicks the taskbar icon, you want to show it and probably Raise it too.
There's some badly formatted code here: http://bytes.com/topic/python/answers/699757-wxpython-how-minimize-taskbar (I've used a variation of it myself, so I know it works).
And here's some information on Task bar icons: http://www.blog.pythonlibrary.org/2011/12/13/wxpython-101-creating-taskbar-icons/
SO I am infact doing something very similar to this user posts:
https://stackoverflow.com/questions/6800292/python-ai-and-3d-animation
but it has no answers and I couldn't contact the user.
Basically I have a functioning python script that answers me with an action accordingly to my voice command. (Fetch emails, weather forecast, turn lights ON/OFF, etc), it has been made using the pyspeech library which is pretty darn good.
Now I want to give my programm a "face"! I thought about modelling the face with Blender (have some knowledge and would build up on it) and I know I could animate it, so the lips move and such.
So I want to know if it is at all possible to:
Load the "face" that I made from blender from my main python script (so when my programm start the face would be there on the screen too)
Run from the script the animations such that when for example when my programm says "You're welcome" I would run the animation that the lips move on the face to simulate it is speaking.
I know that blender has a good python integration (maybe correct is to say it is built on?) and that is why I thought it would be a good program to use.
Hope someone can help and tell me if that is at all possible and maybe show me some right way to go, my googling just showed me always python scripting with Blender which is not what I exactly need here... I think...
Cheers,
Flavio
Indeed, what you want is possible.
If all you want is to play pre-rendered animation videos based on decisions on your program, any GUI that allows you to embedd and play video in a widget will do for your application.
You could rool out your own GUI using Pygame (which has video support, but you will need one of the "minor" more or less "amateur" widget toolkits made for pygame to make up the remaining of your application, as pygame is pretty low level.
On a higher level, although I'had not embedded video, I think you could go with PyQT4 (googled a bit, not that many examples either, buthints that there are eamples in QT4 source) or GTK+ (the samething, it looks like there are more examples).
Another option would be to build your application to run inside the Blener Game Engine itself - It offers both a high level Toolkit, and ways to customize behaviors to user actions (even without coding).
The major drawback in doing this is: I don't know which are the options to distribute an application that needs Blender Game Engine nowadays - your users will need to install Blender (but it is likely Blender folks made an easy way to jhandle this).
On the upper hand: you get the most flexibility, it would even be possible to render some sequences in realtime (as opposed to pre-rendered videos) in your app.
One thing: Blender nowadays use Python 3.x - if the other libraries you need are Python 2, you willl need to make one different process for the GUI inside Blender, and exchange data with your application's backeend in Python 2 (for example using jsonrpc or xmlrpc - that is enoguh simple in Python).
I am thinking of writing a python program that runs in the background and can inspect user's GUI events.
My requirements is very simple:
1) When user right click the mouse, it can show an option; and when this option is chosen, my program should know this event.
2) When user select a file and click some predefined key combination, my program should know this event.
What should I do? Is this a GUI program? I am also thinking that, this program maybe a daemon on the machine and can inspect the user's GUI event, but I am not sure how can I do this.
Thanks.
If you're talking about doing this stuff inside of a wxPython program, then it's all pretty simple. There's a PopupMenu widget for the first one and an AcceratorTable for the second one. If you're wanting to catch mouse and keyboard events outside of a wxPython program, then you have to go very low-level and hook into the OS itself, which means that there really isn't any good way to do it cross-platform. You'll probably want to look at ctypes and similar libraries for that sort of thing.
I've been researching this problem a bit and while I don't have a definite answer, I thought it would be good to share what I found.
First, according to the answers for this question, wxPython cannot track nor control the mouse position outside of the area controlled by the wxPython app. However, as we can see in the answers to this question, under certain circumstances it may be possible to create system-wide hot keys that wxPython can see (although we can also see that this is probably not the optimal solution).
So what will work for the OP? Well, build a cross platform application that does what the OP wants seems almost impossible so I will assume the OP is developing for MS Windows. Following the suggestions in those previous two questions, I looked into the PyWin32 API. It's a little hard to find documentation for it but this question offers some helpful links. A little more digging and I found this tutorial for system-wide hotkeys as well as the win32event.WaitForMultipleObjects and win32event.WaitForSingleObject methods which, if the OP can figure out the proper event, may be what the OP needs to catch menu clicks.
I know it's far from a complete answer but hopefully this can serve as a good jumping-off point for further research.