How to remove icon from QMessageBox in PyQt5? - python

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.

Related

Looking for a way to do force caching kivy widgets

I am currently making an embedded multi-touch software using Kivy.
However, our hardware spec is little tight, so I am trying to optimize GUI for better performance.
Then I've found that Kivy Popup is slow when first pops up then gets faster after that. So now I am guessing that Kivy is doing some caching.
So, I want widgets, especially popups and screens since they are very slow, to act like they were opened once before.
I've tried to open and dismiss every popup widgets and go through all the screens when initializing the program. It seems quite effective except that I could not figure out how to hide the screen changing yet, but I am looking for some better approach.
I've looked for kivy document about kivy.cache here
But it is not clear whether this is what I am looking for or not. In addition, it is hard to understand what do I have to do and what to expect as a result. (Seems it does not work in a way that I want anyway)
It would probably be best to find what takes most time during the first startup, to make sure to cache that, instead of preloading every possible widget. One possibility is that it's just loading the default style texture, which you can load by setting the source of any image widget to images/defaulttheme-0.png, the first widget that needs it (that includes popup) will not to load it itself if you do. If you find that even after displaying a Button, the first Popup is still slow to load, then that's certainly something else, maybe running kivy at trace log_level will help seeing what happens last before the slowdown.
python -c kivy:log_level:trace main.py

How to change the input language when an entry gets focused in Gtk Python

I want to change the input language when an entry gets focused (or dialog is shown) in Gtk Python. In my program, a dialog appears many times and the user must enter some texts. In systems which allow different keyboard layout for each window, the user must change the input language each time the dialog is appearing. I am programming on ubuntu ...
My first approach was be to look at Gtk+ input methods.
The Gtk.Entry has the property im_module, which can be used to set the Gtk.IMContext.
There also two blogpost (1, 2) and an extensive Stackoverflow Answer about Gtk+ input methods that may help you.
My second approach was to use xkb.
You could try something like
subprocess.run('sudo setxkbmap -layout ' + entry_lang_str)
And then I remembered that you could also use gsettings.
gsettings set /org/gnome/desktop/input-sources/ sources [('xkb', entr_lang_str)] (also via subprocess.run())
Using gsettings seems to be the easiest way for me.

Background python program inspect GUI events

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.

Python widget/cursor detection?

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...

Embed interactive shell (VTE) in a PyGtk GUI to manipulate its own widgets

I am planning to do the folliwing:
Create a PyGtk GUI (hardcoded, no Glade) with some widgets, and at the bottom of the screen put some sort of VTE (Virtual Terminal Emulator) from where I could manipulate the widgets, for example changing their attributes and calling their methods from the commandline.
The result would be similar to using AutoCAD's commands, only that I would be acting upon the GUI objects.
I have already found very few things about gtk.VteTerminal widget, but not only could not find a working example or make one myself, it also seem to be a system terminal, not a "current session" python terminal where I could run python commands and access GUI objects.
Any suggestion?
Thanks for reading
What you want exists already: GtkParasite. It's meant for debugging, but I'm sure if you wanted it to actually be a part of your application, you could adapt it.
I'm late to the party, but I had a similar problem.
Look here
Virtual Terminal Question
It's an option if you decide to do something different than what you might have already done.

Categories

Resources