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.
Related
I have a QTextEdit acting as a input field of sorts so whenever you click the post button, it posts whatever is in that QTextEdit. However after I hit post, the text edit deselects which is annoying to have to keep clicking on it before I type more. Is there a method or way in PyQT5 to click on that text box automatically in my code without having to manually move my mouse?
I looked into QTextCursor but that appears to be more for the actual text inside of the text box instead of the box itself. If there's a way to do this, I would greatly appreciate it, thank you!
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
)
I am creating a GUI that contains a drop-down box (called an optionmenu in tkinter). I have it populated with entries.
This is what I want to do but can't figure out the proper commands. If a user hovers their mouse over a specific entry in the option menu, I want a hoverbox/balloon box/tooltip (whatever you want to call it) to appear and show a description of the option they are hovering over.
I assume I have to use some sort of event but I am not sure. Does anyone know how?
I have two questions regarding tkinter that I cant seem to find the answer too.
1) I currently have 2 radio buttons. I have programmed them in such a way that when one of them is clicked another definition is called and that creates a regular button on the screen. Now my problem is, if the user toggles between the both radio buttons, each time a new button will be created (instead of just having 1 the first time he toggles the options). Is there any way I can stop the extra buttons from being created if one already exsists?
2) Is there a widget that can easily be used to create check box lists?
EDIT: Sorry, I meant check box list not radio button list.
1> Define you button outside the eventHandler of the radiobutton.
inside the event handler only grid/pack it,whenever the radiobutton is pressed. In this way the button will not be defined multiple times.
N.B. Use fix row & column with the grid if you need.
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.