Automatic focus on showing deletes the placeholder text of a QLineEdit - python

I use PyQt4 and Python 2.7.9.
My program contains a few QLineEdit objects. The problem is that when the program is launched, one of the QLineEdits is being focused automatically, which causes my placeholder text to disappear.
Is there any way to prevent it, or at least don't let it hide the placeholder text?

Another way is
self.this_widget.clearFocus()
after window has been shown. Only in Qt5 placeholder texts are displayed even with focus. So maybe switch to PyQt5.

You can use setFocus to put the focus on a different widget (although, depending on which widget you pick, you might also need to set the focus-policy first):
self.some_other_widget.setFocusPolicy(QtCore.Qt.TabFocus)
self.some_other_widget.setFocus()
Alternatively, if you use Qt Designer to create the GUI, you could edit the tab-order so that the line-edit is not the first in the chain. This can also be done programmatically using QWidget.setTabOrder.

Related

How does Qt Designer work in terms of creating more than 1 dialog per file?

I'm starting to use Qt Designer.
I am trying to create a game, and the first task that I want to do is to create a window where you have to input the name of the map that you want to load. If the map exists, I then switch to the main game window, and if the name of the map doesn't exist, I want to display a popup window that tells the user that the name of the map they wrote is not valid.
I'm a bit confused with the part of showing the "not valid" pop-up window.
I realized that I have two options:
Creating 2 separated .ui files, and with the help of the .show() and .hide() commands show the correspoding window if the user input is invalid.
The other option that I'm thinking of creating both windows in the same .ui file, which seems to be a better option, but I don't really know how to work with windows that come from the same file. Should I create a separate class for each of the windows that come from the Qt Designer file? If not, how can I access both windows from the same class?
Your second option seems impossible, it would be great to share the .ui since in my years that I have worked with Qt Designer I have not been able to implement what you point out.
An .ui is an XML file that describes the elements and their properties that will be used to create a class that is used to fill a particular widget. So considering the above, your second option is impossible.
This concludes that the only viable option is its first method.

How to make QPushButton editable text when one click on it?

Using Python 2.7 and PyQt4.
So I need a way to make a text of the QPushButton editable when click on it, like on QTextEdit.
There is no builtin way to edit a push button in the sense that you have a cursor and can type along.
Probably the easiest solution is to bring up a QInputDialog. If that feels to heavy, you could also place a floating QLineEdit over or next to the QPushButton. Close that on <Enter> and set the typed text to the QPushButton.
If you really want an editable Button, you'll have to subclass QPushButton and implement the desired functionality yourself. To get started with this, you need to reimplement mousePressEvent() for starting your editing mode. Reimplement keyPressEvent() for handling key strokes. If you need to display a cursor, reimplement paintEvent(). I have no particular resource at hand that describes what exactly you have to do, but the terms above should be sufficient to look it up yourself.

Gtk HeaderBar doesn't expand children

I'm using Gtk to build an application on Linux using Python 3. I'm trying to use a Gtk.HeaderBar. So far it's been working Ok, but it seems that I can't get it to expand it's child widgets. For example:
As you can see above, I've tried putting my Gtk.Entry into the Gtk.HeaderBar, but even with things like Gtk.Entry.set_hexpand(True) it simply refuses to expand. I've even tried putting it inside a Gtk.Box, expanding the Gtk.Box, then adding the Gtk.Entry inside that. Even when I set the Gtk.Entry as a custom title for the Gtk.HeaderBar, this happens:
What's causing this? How can I fix it?
Enabling hexpand only says that you want the widget to be allocated all the remaining space; it does not actually resize your widget. You want the halign property set to GTK_ALIGN_FILL (or whatever it's called in Python) in addition to hexpand.
Check the diagrams on this page for a visual explanation.
You can use Box instead of Headerbar with window.set_titlebar method

How to get multiple panels in one window - GTK+

I am looking to know what is the best practice to make a window which the content changes, but without changing the window. Something like using tabs, but with no tabs, controlled with buttons.
What widget should i use to archive what i need?
And if you don't mind the little off-topic, should it be drawn manually or with a GUI designer like glade?
It is meant to be used within python.
If you can use GTK 3.10, take a look at GtkStack and GtkStackSwitcher. If not, use GtkNotebook and set the show_tabs property to False, then build your own buttons.

Tkinter: Listbox separators, disabled items, keyboard navigation?

I'm studying the Tkinter Listbox widget and have been unable to find solutions for the following functionality:
How can I create non-selectable horizontal separator items, eg. separators equivalent to the Tkinter Menu widget's .add_separator()? (Using chars like dashes and underscores looks awful).
How can I disable a specific item? I tried using .itemconfig( index, state='disabled' ) without success.
How can I enable keyboard navigation, eg. when a user's keyboard input automatically scrolls one forward to the closest item that begins with the text the user typed? Must I bind(<KeyPress>, ...) and manage this behavior myself?
Would some of the above functionality be easier to implement using a Text widget or the ttk.Treeview widget?
you cannot. The widget doesn't support that.
you can't disable certain items, the widget doesn't support a state attribute. That being said, you can monitor the selection and do the appropriate thing if the user selects something that is disabled, and use the item foreground to denote disabled-ness.
You will need to bind to keypress events and manage the behavior yourself. It's not particularly difficult, just a little tedious.
the text widget might be your best bet, though you'll have to add bindings to mimic the default bindings of the listbox.
Bottom line: Tkinter provides nothing that directly supports what you want to do, but the building blocks are all there. You'll just have to build it yourself.

Categories

Resources