PyQt save to clipboard in a slot - pyqt

How can I save some text to clipboard by pressing button? clipboard.setText("gg") works by itself
widget.connect(button, QtCore.SIGNAL('clicked()'), clipboard.setText("text") )
throw error, you can only use instance.methodName
widget.connect(button, QtCore.SIGNAL('clicked()'), clipboard, QtCore.SLOT('setText("text")') )
do nothing.
What is wrong?

First, there's a much better way to connect signals to slots on PyQt:
button.clicked.connect(self.method)
You can use lambda functions to pass extra arguments to methods.
Then you call
button1.clicked.connect(lambda : clipboard.setText('btn one'))
button2.clicked.connect(lambda : clipboard.setText('btn two'))
When you pass a function call, in fact the interpreter is evaluating the call and trying to pass the result to the SIGNAL/SLOT connection. That's why your first example doesn't work.
I've written something similar here: https://stackoverflow.com/questions/...from-other-functions

Related

How to use Tkcalendar virtual events?

Still I’m confused on that tkcalendar virtual events usage to bind user actions on the calendar days selections. Making something like follow,
calendar.bind("<<CalendarSelected>>", print(calendar.get_date())
That print the default date once the I run the script but when I select on the displayed calendar nothing is printed. Normally any selection on any day on the calendar should be printed if was really bound. I did something wrong right ?
You need to use a lambda function instead
calendar.bind("<<CalendarSelected>>", lambda: print(calendar.get_date())
You code will currently bind the result of the print function as the callback for the bind. Since print returns None, no function will be called.
A lambda creates an anonymous function.
If your code gets more complex than a single line, put this code in to a function of its own and use the name of that function in the bind method, for example
calendar.bind("<<CalendarSelected>>", showDateToUser)

Run OpenGL window besides Tkinter window

I am trying to open :
on one hand, an OpenGL window, using glfw
on the other hand, a GUI window, using tkinter
However, both are run with a blocking call for them to render:
glfw: while not glfw.window_should_close(window): ...
Tkinter: self.ui_root.mainloop()
Would you know a way to circumvent that, and to allow to have both windows open at the same time, accepting Keyboard/Mouse events?
If you want to run the tkinter window on a keyboard event, you have to write a callback function to do this. Suppose, if you press "T" then the tkinter window will be showed up. According to your question it will run self.ui_root.mainloop(). Here, the basic structure of your code will be this:
#callback function
def key_input_callback(window, key, scancode, action, mode):
if key == glfw.KEY_T and action == glfw.PRESS:
self.ui_root.mainloop()
#display loop
while not glfw.window_should_close(window):
....
....
glfw.set_key_callback(self.window, key_input_callback)
....
....
Here, the callback function receives the keyboard key and the action, and perform what you want it to do. The action is one of glfw.PRESS, glfw.REPEAT, or glfw.RELEASE.

Control close event pyglet

I have a program that has multiple windows in pyglet, and I want to make one window unclosable, rather to set its visibility to false. Is there a way I can access the event handle for close, to make it instead of closing the window, possibly return a bool or a string like "should close."?
Found the answer to my own question, first define a custom event loop with window_event_loop = pyglet.app.EventLoop(), then create a handler for .event for the event loop
#window_event_loop.event
def on_window_close(window):
# Extra code here
return pyglet.event.EVENT_HANDLED

nightwatch - check for popup window, after each click event

Is there a way in nightwatch to check whether a popup window appears after each click event?
I have a problem that randomly an error message appear and I don't want to write for each click event the same callback function.
I have already tried out the after and afterEach commands in the Global.js but then the commands will only run after the whole test suite.
I also have tried it local within a test file, although it also does not cover all single click events, even though the official website writes "... while beforeEach and afterEach are ran before and after each testcase (test step)"?
Solution I'm looking for:
.waitForElementVisible('selector')
.click('selector')
.click('selector')
Solution I have come up with so far:
.waitForElementVisible('selector')
.click('selector', isPresent)
.click('selector', isPresent)
isPresent as a callback function, which does the checking and close the popup window if it appears.
Is there another way to write a function (with or without after and/or forEach), so that it will be called after each click event or after each command. Thus, I don't have to write the isPresent repetitive code?
You can insert something like this in your page object file:
var popupCommand = {
popupCheck:function(){
return this.waitForElementVisible('selector', 5000)
.click('selector', isPresent)
.click('selector', isPresent)
},
module.exports = {
commands:[popupCommand],
elements:{
firstElement: {selector: 'xpath',locateStrategy: 'xpath'},
secondElement: {selector: 'css'},
}
}
Where 'popupCommand' will be the name of your page object file, for example 'Popup'. And also you will have to insert your isPresent callback function here so you can use it.
I did my best to explain you as much as possible what and how to do that :)
you should yse .switchWindow() method.
Why don't you write your own custom command specific for that case, so that way you will avoid repetitive code?

Terminating each() block in Protractor

I was automating the an application (using Protractor) and I have come across situation where I wanted to select the an option from the type ahead using the DOWN arrow button from the Keyboard. Here is how I am approaching to this action.
After typing part into the text field I am getting the reference of each option that appear in the type ahead.
Now, I am using .each() method of protractor to iterate through each of the option to look for the required option.
I'm making the script to hit DOWN arrow button to iterate through each option in the type ahead.
Suppose there are 10 options displayed in the type ahead and the option that I need to select is at 5th position. Now when I reach the 5th position I am selecting the option but each() function still continues.
I want the loop to terminate when required option is selected. Something like BREAK statement in FOR loops.
BTW I have tried the above scenario with FOR loop but unable to use BREAK statement within then() handler.
Please let me know how to cope up with this situation.
You could throw an exception to terminate the loop. Put the loop inside try and use catch to wrangle your results. You can also just use a boolean variable to indicate that you have found a match and ignore everything after that point. I would just use a for loop though.
Edit:
You could add a variable to hold an action before the allBenchmarks.each
var action
Then inside the test
if(dataValue == optionToSelect){
action = function() {benchmark.click(); ...}
}
After the loop exits call the action
if (action) action()

Resources