Is it possible to make Gtk IconView (in pygtk) allow selection of multiple icons without the Ctrl key being pressed?
I basically want the behaviour of Ctrl being held down even when it is not held down.
Overriding this kind of behaviour might confuse users. But if you really want to, there are two possibilities that I can see:
Either make the IconView believe Ctrl is always pressed:
def force_ctrl(iv, ev): ev.state |= gtk.gdk.CONTROL_MASK
iconview.connect('key-press-event', force_ctrl)
iconview.connect('button-press-event', force_ctrl)
Or you could try implementing the selection behaviour yourself, something like:
def clicked(iv, ev):
p = iv.get_path_at_pos(int(ev.x), int(ev.y))
if not p is None:
if iv.path_is_selected(p):
iv.unselect_path(p)
else:
iv.select_path(p)
return True # make the IconView ignore this click
iconview.connect('button-press-event', clicked)
Related
I have a button called "start" and when I click the button, it becomes "Pause", when I click it again, it doesn't revert back to "start" it just stays as is and I need to be able to have it go back to the original text. How do I go about this? Right now I have this
def button_click(self):
self.start_button.setText("Pause")
This changes the start button to pause but I don't know how to go from pause to start.
Once you set a property on an object, it doesn't "remember" its previous state, unless you provide that feature in some way.
A simple solution, at least for boolean-like objects, would be to store the current state, and toggle between them.
self.start_button = QPushButton('Start')
self.start_state = False
# ...
def button_click(self):
self.start_state = not self.start_state
if self.start_state:
self.start_button.setText("Pause")
else:
self.start_button.setText("Start")
An alternative could be to use a checkable button instead, which is also more clear from the UX perspective as the pressed/unpressed state is much more visible to the eye than a string (imagine using "Start"/"Stop", which are very similar).
Since the clicked also sends the checked state automatically, you can add the argument to the function you already have and check that instead.
self.start_button = QPushButton('Start')
self.start_button.setCheckable(True)
# ...
def button_click(self, state):
if state:
self.start_button.setText("Pause")
else:
self.start_button.setText("Start")
Note that for this it's usually preferred to use the toggled signal instead, since it also reacts to setChecked() and toggle() functions.
I don't think there's a nice fancy way to do this. Probably just do the following:
def button_click(self):
if self.start_button.text() == "Pause":
self.start_button.setText("start")
elif self.start_button.text() == "start":
self.start_button.setText("Pause")
I have a GUI written in tkinter and all works fine. I want to enhance it so that when a user left clicks on a certain tab with the mouse, a method is executed. I thought this would be straight forward but I can't get it working. My code is
def f_x():
print('entered this method')
tab4e = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)
tab4e.bind("<Button-1>",f_x())
When the tab changes, it emits the event "<<NotebookTabChanged>>", which you can bind to:
def handle_tab_changed(event):
selection = event.widget.select()
tab = event.widget.tab(selection, "text")
print("text:", tab)
notebook = ttk.Notebook(...)
...
notebook.bind("<<NotebookTabChanged>>", handle_tab_changed)
The benefit of using this event rather than binding to a mouse click is that the binding will fire no matter what causes the tab to change. For example, if you define shortcut keys to switch tabs, binding to the mouse won't cause your handler to fire if the user uses one of those shortcut keys.
You were right, this is pretty straight forward, what you did was almost correct, you need to pass the function not the return value of the function to bind. So you will need to get rid of the parentheses after f_x. Another thing is, the bind also automatically pass an argument to the callback called event, so you will need to let f_x accept an argument.
def f_x(event): # accept the event arg
print('entered this method')
tab4e = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)
tab4e.bind("<Button-1>",f_x) # not f_x()
This works now detecting when a TAB Canvas has been brought to focus through a LEFT mouse click
def fTabSwitched(event):
global notebook2
l_tabText = notebook2.tab(notebook2.select(), "text")
if (l_tabText == 'eee'):
print('lets roll')
tab4a = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)
tab4b = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)
tab4c = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)
tab4d = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)
tab4e = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)
notebook2.add(tab4a,text='aaa')
notebook2.add(tab4b,text='bbb')
notebook2.add(tab4c,text='ccc')
notebook2.add(tab4d,text='ddd')
notebook2.add(tab4e,text='eee')
notebook2.bind("<ButtonRelease-1>",fTabSwitched) # must be release
I am developing an app.I would like there to be an option to zoom in and out. to do this I would like to have the program get when the control key is clicked and the mouse is being scrolled at the same time, and zoom accordingly(scroll up for zoom in, and scroll down for zoom out) I've been doing a lot of searching and reading,but only found ways to get a control press and a mouse click-event, not a mouse scroll-event. I also found a way to get a mouse scroll but couldn't get it to work with just control press. - and to ignore all other presses.
Could anyone suggest something to help me???
It depends a bit on the structure of your application.
One way to get scroll-events is to add a wheelEvent handler to your Widget
def wheelEvent(self, QWheelEvent):
modifiers = QtGui.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.ControlModifier:
# do your processing
Another approach could be to install an eventFilter in the component where you want to intercept the scroll-events by
component.viewport().installEventFilter(self)
(maybe you have to install the event filter on the component itself iso on the viewport).
and self has an eventFilter function like
def eventFilter(self, qobject, event):
if (event.type() == QtCore.QEvent.Wheel) :
modifiers = QtGui.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.ControlModifier:
#do some scaling stuff
return True
return False
else:
# standard event processing
return False
Hope this helps.
I got a couple of questions regarding qDialogButtonBox. While my code still works, I believed that there are a few parts that can be better refined/ I am not finding much info online
class testDialog(QtGui.QDialog):
def __init_(self, parent=None):
...
self.init_ui()
self.signals_connection()
def init_ui(self):
...
self.buttonBox = QtGui.QDialogButtonBox()
self.buttonBox.addButton("Help", QtGui.QDialogButtonBox.HelpRole)
self.buttonBox.addButton("Apply", QtGui.QDialogButtonBox.AcceptRole)
self.buttonBox.addButton("Cancel", QtGui.QDialogButtonBox.RejectRole)
#
def signals_connection(self):
self.test_random.clicked.connect(self.test_rand)
# Is this the latest/correct way to write it?
self.buttonBox.accepted.connect(self.test_apply)
self.buttonBox.rejected.connect(self.test_cancel)
self.buttonBox.helpRequested.connect(self.test_help)
def test_apply(self):
print "I am clicking on Apply"
def test_cancel(self):
print "I am clicking on Cancel"
self.close()
def test_help(self):
print "I am clicking for Help!"
My questions are as follows:
Under my function - signals_connection(), the lines that I wrote for
the buttonBox (though the code still works) are quite different
for the signal I have wrote for the self.test_random and I am
unable to find any similar online for the qdialogbuttonbox.. There
is another style that I have found - self.connect(self.buttonBox,
QtCore.SIGNAL("accepted()"), self, QtCore.SLOT("accept()")) but I
think that is the old style?? Otherwise what should be the right way
to write it?
In my test_cancel() function, is writing self.close() the best
way to close the application? The way that I run my program is as
follows:
dialog = testDialog();dialog.show()
Lastly, is it possible to add 3 different tool tips to the 3 buttons I have created? I saw that there is a command for it - self.buttonBox.setToolTip("Buttons for life!"), but this will results in all 3 buttons to have the same tool tip. Can I make it as individual?
Yes, that is the correct way to write signal connections (the other syntax you found is indeed the old way of doing it). You can find all the signals in the pyqt documentation for QDialogButtonBox. Different widgets and objects have different signals. QPushButton's and QDialogButtonBox's have different signals.
Yes, close() will close the dialog. The QApplication will exit by default if there are no other windows open. However, if this is a modal dialog, you typically want to close a dialog with either the accept or reject command. This will alert the calling function as to whether the dialog was closed with the Ok/Yes/Apply button or closed with the No/Cancel button.
You can set different tooltips for different buttons in the QDialogButtonBox. You just need to get a reference to the specific button you want to set the tooltip for.
For example
self.buttonBox.button(QDialogButtonBox.Help).setToolTip('Help Tooltip')
self.buttonBox.button(QDialogButtonBox.Ok).setToolTip('Apply Tooltip')
Or you could loop through all the buttons
for button in self.buttonBox.buttons():
if button.text() == 'Help':
button.setToolTip('Help Tooltip')
elif button.text() == 'Apply':
button.setToolTip('Apply Tooltip')
Also, you could connect the accepted and rejected signals from the QDialogButtonBox to the accept and reject slots on the QDialog
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
That way, you won't have to manually connect the Ok and Cancel buttons to your callbacks for closing the dialog.
I have a button which sets/unsets spellcheck highlighting in a QTextEdit box (ref PyQt - How to turn on/off spellchecking) which works fine.
Then I added a language selection QComboBox and tied its signal to the button's property but its highlighting set/unset doesn't work on changing the language. It drives me nuts, there may be something small and stupid I've done, but for the sake of it I can't find anything wrong with it.
The button (action rather) is
self.actionSpellCheck = QAction(QIcon(self.icon_spellcheck),
"Auto &Spellcheck", self,
shortcut=Qt.CTRL + Qt.SHIFT + Qt.Key_O,
triggered=self.spellcheck, checkable=True)
The combobox is
self.cb_lang = QComboBox(tb)
tb.addWidget(self.cb_lang)
lang_list = self.dict_broker.list_languages()
self.cb_lang.addItems(lang_list)
self.cb_lang.currentIndexChanged.connect(self.spellcheck)
and the self.spellcheck is
def spellcheck(self):
pos = self.cursor.position()
if self.actionSpellCheck.isChecked():
lang = self.cb_lang.currentText()
self.dict = self.dict_broker.request_dict(lang)
self.highlighter.setDict(self.dict)
self.setHighlighterEnabled(True)
self.show_status("Spellcheck language is set to " + self.dict.tag, None)
else:
self.setHighlighterEnabled(False)
self.highlighter.setDict(None)
self.show_status("Spellcheck is turned off", None)
self.cursor.setPosition(pos, QTextCursor.MoveAnchor)
self.textEdit.setTextCursor(self.cursor)
self.textEdit.setFocus()
How come the highlighter gets set/unset on clicking the button, but nothing happens on selecting the language (it only happens after I start typing, not immediately on combobox selection)? Thank you.
If you look at the HighLighter.setDict method, you'lll see that it doesn't do much other than reassign the dict attribute.
Also, the SpellTextEdit.setHighlighterEnabled only resets the document.
So you're going to need a method to re-highlight the text whenever the dict changes. Fortunately, HighLighter is a subclass of QSyntaxHighlighter, which already has a rehighlight slot which does what is required.
So you just need to amend your spellcheck method as follows:
def spellcheck(self):
pos = self.cursor.position()
if self.actionSpellCheck.isChecked():
self.setHighlighterEnabled(True)
lang = self.cb_lang.currentText()
self.dict = self.dict_broker.request_dict(lang)
self.highlighter.setDict(self.dict)
self.highlighter.rehighlight()
else:
...