I have a Python 3.6.3 script that incorporates PyQt5. The GUI has a button that toggles play and pause of a video file. Pressing the spacebar also toggles play and pause. The button works fine all the time. The spacebar also works as intended, except at the very start of executing the script. I need to click some other event first (such as a button or slider bar) before the spacebar will correctly triggers the signal to pause or play the video. I want to start playing the video with the spacebar when the script starts up. Here is how I create the connections in the same __init__(self) function . As you can see, clicking on the button and pressing the spacebar both connect the signal to the same function (self.pauseVideo).
self.btnPause = QtWidgets.QPushButton(self.centralwidget)
self.btnPause.clicked.connect(self.pauseVideo)
self.spacebar = QShortcut(QKeySequence(Qt.Key_Space), self)
self.spacebar.activated.connect(self.pauseVideo)
Suggestions on how to get the spacebar to trigger the signal when the script first starts?
After a lot more searching, reassigning keyboard shortcuts (without success), etc., I found this post PyQt widget keyboard focus. I inserted
self.setFocusPolicy(Qt.StrongFocus)
into __init__(self) and it seemed to solve my problem! Now I'm just hoping it doesn't cause other issues.
Related
I have a simple copy paste event, which should trigger a function while my program runs:
root = tkinter.Tk()
root.bind('<Control-c>', parse_item)
root.mainloop()
parse_item grabs the contents of the clipboard and makes some calculations.
The problem I have is:
It only works if my program is in focus. I need to be with the mouse in the program, then the bind event will trigger.
What I need:
The bind event should also trigger if the program is not in focus (for example the program is minimized). It should always trigger while the program runs.
It only works if my program is in focus. I need to be with the mouse in the program, then the bind event will trigger.
You cannot do that with tkinter. It can only operate on events in widgets that it controls, and only when it has focus. You would have to use a platform-specific tool to intercept events from other programs.
I'm making a program that will do multiple keyboard actions when I press my own shortcut (ctrl+q). How do I make my program listen to the binds when the program is in the background?
def pasteFun(event):
messagebox.showinfo("hey")
root.bind("<Control-q>", pasteFun)
This works fine when I am in the program, but when I minimize it, ctrl+q does nothing.
def test(event):
messagebox.showinfo("hey","hey")
root.bind_all("<Control-q>",test)
I have tried root.bind, frame.bind, and root.bind_all, but my bind does nothing when the program is in the background/minimized.
I need a function to run when I'm outside of the program and hit my binds/shortcuts.
I'm making a program that will do multiple keyboard actions when I press my own shortcut (ctrl+q). How do I make my program listen to the binds when the program is in the background?
You can't do that with tkinter. Tkinter will only see events when it has focus. That is a fundamental part of its design.
I am about to switch from Windows to Ubuntu. Since my mouse keeps doing multiple clicks each time I press the middle mouse button, I used AutoHotkey under Windows to add a delay after each click. This worked fine. Now under Ubuntu I want to use AutoKey to do the same. Autokey uses Python for its scripts though.
Here is the AutoHotkey script:
MButton::
If (A_TimeSincePriorHotkey < 200)
Return
Send {MButton}
Return
Currently, (as of version 0.95.4), this is not possible from within AutoKey, because it can’t handle mouse buttons as hotkeys.
This stackoverflow question may be of help: Triggering AutoKey Script via Mouse Button - How To?
I press this tkinter button, and the function bound to it finishes, but the button is stuck down. The rest of the gui is fine, its responsive, everything normal except for the button that is stuck down. I can even press the "stuck down" button again, and the bound function executes and finishes, but the button is still stuck down.
Sometimes the first few times I click the button it works fine and the button comes back up, but then after a few more clicks it will be stuck down. What could be causing this?
Here is the code for the button:
bf1=Button(self.canvas,text='F1',fg='tan1')
bf1.grid(row=4,column=1,sticky='nwse',columnspan=4)
bf1.bind('<Button-1>',self.f1)
I have found what is causing this, it is the fact that I am using the 'bind' function instead of the 'command'.
When I use this, the button sticks if you move the mouse off the button before the callback finishes (you can only realistically do this if you have a lot of work to do in the callback):
bf1=Button(self.canvas,text='F1',fg='tan1')
bf1.grid(row=4,column=1,sticky='nwse',columnspan=4)
bf1.bind('<Button-1>',self.f1)
But if you use the 'command', then there is no problem:
bf1=Button(self.canvas,text='F1',fg='tan1',command=self.f1)
bf1.grid(row=4,column=1,sticky='nwse',columnspan=4)
Is this a bug with tkinter's 'bind'?
I am showing a user a popup menu when they right click.
According to the popup behaviour, I want to destroy the popup when it looses focus(when user clicks outside the popup window). For that I tried to connect the popup window to "focus-out-event", using Glade. But for some reason, that event is not getting fired. I tried to print something on the terminal when the event is fired, but nothing gets printed.
I am new to Python and GTK, and now I have no clue how to proceed further.
#handler to catch the focus out event
def on_popup_menu_add_attachment_focus_out_event(self, *args):
print("Destroying the popup.....")
focus-out-signal and its handler
I just changed the Type as Top Level and it worked!