Turn QPushButton on and off - python

I have an app in PyQt with a few buttons to shift between modes of the application.
I have the clicked() signals of the buttons linked to the appropriate methods. My problem is that there are other ways to change modes (for instance, loading settings will automatically move the user to their default mode), and I'm using the QPushButtons as indicators of the "active mode". This was previously accomplished by having two icons for each button, one for the button being off and the other for the button being on. It's all been designed in QtDesigner, so clicking on one button turns it on (and turns the other buttons off) and changes the icons appropriately. And when the mouse is released, that button stays on.
The button stays on until another button is pressed.
I'm trying to figure out how I can change a button from "on" to "off" without the user actually pressing the button, so I can change modes appropriately.
The Qt docs make reference to the property I'm looking for but I can't find any more details than the fact that these states exist:
The most important modes or states are:
Available or not (grayed out, disabled).
Standard push button, toggling push button or menu button.
On or off (only for toggling push buttons).
Default or normal. The default button in a dialog can generally be "clicked" using the
Enter or Return key.
Auto-repeat or not.
Pressed down or not.
(http://qt-project.org/doc/qt-5/QPushButton.html)
To be more specific, I'm looking for a way to see the state of a QPushButton; to see whether it is "On" or "Off", and I'm looking for a way to change that state.
EDIT: I found the appropriate method QPushButton.isOn() but the problem is that it's in Qt3. (I'm using Qt5, where this method no longer exists). Clearly it's obsolete now, would anyone happen to know what replaced it?
http://doc.qt.digia.com/3.2/qpushbutton.html#isOn

QPushButton inherits from QAbstractButton and and therefore has the following methods: isChecked, setChecked and isCheckable, setCheckable. This way you can convert the button into a toggle button and ask/set the state. There is also setAutoRepeat and autoRepeat which controls the auto repeat programmatically.
Especially instead of isOn use isChecked.
Furthermore it also inherits from QWidget which has methods isEnabled and setEnabled. With this you can activate/de-active the button which is shown by graying out the button as well as by prohibiting clicks on the button.
Basically just study the documentation for QAbstractButton and QWidget to see how you can programmatically interact with a QPushButton to enable/disable it.

Related

How to set focus to the old window on button click in PyQt5 Python?

My Current Project is to create a On Screen Keyboard for my personal usage with my personal functionality. I made gui and primary function in PyQt5 with Python. I managed to type the letter on button click with pyautogui.write() method. But the problem is, where I want to type there is no focus. suppose I want to write on chrome's address bar or any other input field on my monitor. when I click on button to type a letter, chrome lost focus. I want to set focus to the old window while press on any button. I searched on google about this but didn't found any answer. How can i set focus to old window? or is there any better way to type on focus lost state?
You should not try to "set the focus back", as it would be almost impossible to know what window had the focus before (and a new window might raise in the meantime). What you should actually do is to prevent your window to get focus at all, thus avoiding it to steal focus from the others.
In order to achieve this, you must set the appropriate window flag (or initialize the widget with it using the flags keyword argument), which for this is Qt.WindowDoesNotAcceptFocus.
Note that you might also want to set the Qt.WindowStaysOnTopHint in order to always keep your window above the others:
class MyKeyboard(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowFlags(
QtCore.Qt.WindowDoesNotAcceptFocus
| QtCore.Qt.WindowStaysOnTopHint
)

Have a popupmenu appear on a popupmenu on right-click

I have a wx Popupmenu appear when I left-click on a toolbar LabelTool. I already have a binding to have a handler run when I left click an item in the menu, but I want another popup menu to appear when I right click an item in the original popup menu. I've already tried binding it to wx.EVT_RIGHT_DOWN but it doesn't do it. Whenever I try right clicking on a menu object, it seems it still calls the wx.EVT_MENU, which calls the same handler as a left-click would. How would I implement this?
As far as I can tell, that is not supported. Besides, no user is going to expect that they need to right click on a context menu anyway. You should rethink your design so it's more intuitive. Perhaps by using a sub-menu instead of a secondary popup menu

PyQt Button Selection

I have a PyQt GUI set up that has a selection of QPushButtons and a QLineEdit text box (among other things). The text box is set up so as to call a function upon returnPressed(). My problem is that when I click on the text box and put in text, one of the buttons becomes selected which means that when I press enter in the text box it activates both the button and the text box function.
Is there a way around this? Some way to stop any buttons from being selected while the text box is being edited?
The code is fairly long so I can't add it here but if there are any questions regarding layout or anything that may be relevant, please ask.
Thank you for any help you can offer
From your question and comments, I'm guessing that the buttons and line-edit are in a QDialog, and that the selection/highlighting occurs due to the default/autoDefault property of buttons.
Normally, these properties will be set to False, but in a QDialog they are automatically set to True. The button that is the current default gets an additional frame drawn around it (even when it doesn't have the keyboard focus), and is activated whenever the return key is pressed.
You can of course prevent this behaviour by simply doing:
button.setDefault(False)
button.setAutoDefault(False)
for each button in the dialog.

How to get clicks on disabled buttons with wxpython?

I have a disabled button, and it does not receive clicks when I use EVT_BUTTON on it. Is there a way to receive clicks even when it has been Disabled()?
The whole point of disabling a button is so that the EVT_BUTTON event is not fired. I'm sure you could create create an ugly hack using EVT_LEFT_DOWN and detecting where the mouse is in your app as a workaround, but why bother? This is intended behavior.
Perhaps wxpython has a mechanism similar to pygtk.
In pygtk you create a input-only (that is transparent) window over the widget you want to get clicks for and get your clicks there.

Change the focus from one Text widget to another

I'm new to Python and I'm trying to create a simple GUI using Tkinter.
So often in many user interfaces, hitting the tab button will change the focus from one Text widget to another. Whenever I'm in a Text widget, tab only indents the text cursor.
Does anyone know if this is configurable?
This is very easy to do with Tkinter.
There are a couple of things that have to happen to make this work. First, you need to make sure that the standard behavior doesn't happen. That is, you don't want tab to both insert a tab and move focus to the next widget. By default events are processed by a specific widget prior to where the standard behavior occurs (typically in class bindings). Tk has a simple built-in mechanism to stop events from further processing.
Second, you need to make sure you send focus to the appropriate widget. There is built-in support for determining what the next widget is.
For example:
def focus_next_window(event):
event.widget.tk_focusNext().focus()
return("break")
text_widget=Text(...)
text_widget.bind("<Tab>", focus_next_window)
Important points about this code:
The method tk_focusNext() returns the next widget in the keyboard traversal hierarchy.
the method focus() sets the focus to that widget
returning "break" is critical in that it prevents the class binding from firing. It is this class binding that inserts the tab character, which you don't want.
If you want this behavior for all text widgets in an application you can use the bind_class() method instead of bind() to make this binding affect all text widgets.
You can also have the binding send focus to a very specific widget but I recommend sticking with the default traversal order, then make sure the traversal order is correct.
It is really simple in PyQt4 simply use this one single line below and you will be able to change focus by pressing tab button:
self.textEdit.setTabChangesFocus(True)
The focus traversal is somewhat customizable, usually letting the X windows manager handle it (with focus follows mouse, or click). According to the manual it should be possible to bind an event to the key press event, for tab presses, and triggering a focusNext event in those cases.

Categories