my excuses if that question is very simple. I have a GUI with a few pushbuttons (QT with PySide). I want to connect 2 buttons with one function which is supposed to do the same thing with one difference. For that I need to pass an argument. Here is the type of code:
def test(flag):
if flag:
do something
else:
do something else
# button calls:
self.ui.button1.clicked.connect(test(True))
self.ui.button2.clicked.connect(test(False))
I have tried things as above, but it does not like the test(True). I have also played with lambda's and I am getting stuck at the same spot. So far I have been calling two separate functions, but that is not a good practice. Any help is appreciated.
Thanks
It works! Much simpler than I thought. Many thanks.
You said you used lambdas, did you try this
self.ui.button1.clicked.connect(lambda:test(True))
self.ui.button2.clicked.connect(lambda:test(False))
This should work.
Related
I am currently building an interface using PyQt5 and I was wondering if there is an elegant way to connect several signals to the same function?
The straight-forward solution is this:
self.ui.button1.clicked.connect(self.Function)
self.ui.button2.clicked.connect(self.Function)
self.ui.button3.clicked.connect(self.Function)
self.ui.button4.clicked.connect(self.Function)
but is there a nicer, more readable option? For example something that would look like:
self.Function.connect(self.ui.button1.clicked,
self.ui.button2.clicked,
self.ui.button3.clicked,
self.ui.button4.clicked)
Also I started reading about QSignalMappers but if they can be avoided it's nice.
Thank you in advance!
You could make a function that takes a tuple or list of buttons as an argument, then connects the click signal for each one.
self.connect_buttons((self.ui.button1, self.ui.button2, self.ui.button3, self.ui.button4))
def connect_buttons(self, button_tup):
for button in button_tup:
button.clicked.connect(self.Function)
Trying my first foray into Python and TkInter, and have already gotten some great help here. However, I'm hitting another wall and hoping somebody here can give me a lift to the other side.
My app is pretty simple, conceptually. Just a page with 10-20 rows of labels next to text boxes and option menus (with about 25 items, all identical).
All I want to do is add SUBMIT and QUIT buttons. But, as with most things I'm trying, I get errors every step of they way.
SubmitButton = Button(root, text="Submit", command=greetings)
SubmitButton.place(x=700, y=600)
def greetings():
print("Hello there")
The error (on this particular example) is
NameError: name 'greetings' is not defined
But isn't that what def greetings() does?
Most of the examples I'm seeing refer to things like self/master/frame, but I don't have any of that stuff in my script (at least not explicitly).
Isn't there some easy way to create a custom function and call that function on the click event? Or do I have to go back to the drawing board and try to understand how to use classes and such?
I'm using Python 3.6 and Spyder if that makes any difference.
Thank you for any guidance.
I'm not sure why it's not printing to the console on button push and without seeing your updated snippet I wouldn't be able to tell.
However, the below snippet does work for me and should demonstrate how to handle custom functions on buttons:
from tkinter import *
root = Tk()
def greetings():
print("Hello there")
SubmitButton = Button(root, text="Submit", command=greetings)
SubmitButton.pack()
Also, to answer your question. No, you don't need to use classes or any form of object orientation. However, when it comes to tkinter (and yes this is heavily opinion based) it is often easier to use classes, there are countless reasons why this is and there are plenty of questions and answers on and off this site as to why this is.
This answer probably has the best explanation as to why this is.
I'll preface this by saying that I'm very inexperienced with python, and I'm hoping that means the solution to my problem will be simple.
My program will be performing simple actions in another window, so ideally I would like my script to make sure that this other window is maximized and active before it proceeds with the rest of the commands. This has proved to be much more difficult than I had expected.
I am fairly certain that I can achieve this with win32gui using find_window and setting it to the foreground. I thought I had found my solution when I came across this previous question:
Python Window Activation
Unfortunately I was unable to use the solution code or manipulate it to solve my problem for a few reasons:
-The way that user had defined find_window only allows you to choose by the classname of the window, which I don't know and haven't been able to find since it is just a game running in Java. I couldn't seem to change that line to work for the specific windowname (Which I do know) as it is not the "default argument".
-I don't want to enumerate the windows to find it as I'm not sure how that would work.
-using find_window_wildcard as it's written in that script has managed to bring the window to the foreground a few times, but only if the window was already open, and it only works intermittently.
-set_foreground() requires one input statement and no matter how I try to set it I am always given an error that I either have the wrong number of elements or an invalid handle on the window.
I know that I'm stupid; but a clear and concise solution to this problem or at least a good explanation of the find_window/getwindow syntax would be a godsend to myself and anyone else who's having this problem.
I would strongly suggest you take a look at the pages for Swapy and for pywinauto. They can help you do some awesome things when it comes to UI Automation.
The way that user had defined find_window only allows you to choose by the classname of the window
The way the user defined it is to pass the two parameters class_name and window_name through, untouched, to win32gui.FindWindow (which in turn just calls the Win32 API function FindWindow). So, just do this:
windowmgr.find_window(None, 'My Window Name')
But even if that weren't true, you don't need to use his find_window function; it should be pretty obvious how to call win32gui.FindWindow yourself:
hwnd = win32gui.FindWindow(None, 'My Window Name')
And if you want a good explanation of the FindWindow/EnumWindows/etc. syntax, did you try looking at the docs for them? Is there something you didn't understand there?
Meanwhile:
… the classname of the window, which I don't know and haven't been able to find since it is just a game running in Java
What difference does it make that it's running in Java? You can enumerate the windows and print out their classnames, whether they're written in C++, Java, .NET, Python, or anything else. Or use one of the tools that comes with Visual Studio/VS Express, or any of the free improved versions you can find all over the net, like MS Spy++, which will let you point at a window and give you the caption and class name.
I don't want to enumerate the windows to find it as I'm not sure how that would work.
Just call windowmgr.find_window_wildcard(wildcard) with a regular expression, and it'll enumerate the windows and compare their titles to that regular expression.
If you want to write your own code to do it, just write a function like this:
def my_callback(hwnd, cookie):
Now, when you do this:
win32gui.EnumWindows(my_callback, some_cookie)
… it will call your my_callback function once per window, with hwnd being the window (which you can pass to win32gui functions like, e.g., GetWindowText), and cookie being the same some_cookie value you passed in. (If you don't need anything passed in, just pass None, and don't do anything with the value in your callback function. But you can see how the other answerer used it to pass the regular expression through.)
Meanwhile:
using find_window_wildcard as it's written in that script has managed to bring the window to the foreground a few times, but only if the window was already open, and it only works intermittently.
First, you can't bring a window to the foreground if it doesn't exist. How do you expect that to work?
As far as working intermittently, my guess is that there are lots of windows that match your wildcard, and the program is going to just pick one of them arbitrarily. It may not be the one you want. (It may even be a hidden window or something, so you won't see anything happen at all.)
At any rate, you don't need to use find_window_wildcard; if you know the exact name, use that. Of course it still may not be unique (whatever the game's name is, there's nothing stopping you from opening an email message or a Notepad window with the same title… which, by the way, is why you want to try class names first), but at least it's more likely to be unique than some underspecified wildcard.
So, what if the class name isn't unique (or, even worse, it's one of the special "number" classes, like #32770 for a general dialog window), and neither is the window name? You may be able to narrow things down a little better by looking at, say, the owning process or module (exe/dll), or the parent window, or whatever. You'll have to look through the win32gui and/or MSDN docs (linked above) for likely things to try, and play around through trial and error (remember the Spy tool, too) until you find some way to specify the window uniquely. Then code that up.
If there are lets say 4 buttons, all with the same Click event, how can I find out which button was pressed?
if the event looks like this def Button_Click(self, sender, e): I'm sure I can compare sender to my buttons somehow. But how?
Well, I've never used IronPython so I don't know how much help this will be, but what I usually do when trying to figure out these things in regular python is to print type(sender) , print sender and print dir(sender) to console(or output to a file if you don't have a console available).
This should help you figure out what exactly is the "sender" parameter. In the simplest case it could be the button itself so a simple == will work to know which button it was. Or it could have a method/property that gets you the button object. In which case, dir(sender) might contain an obvious one, or if not, google the class name gotten from type(sender) and see if you can find any docs.
How can I do it, even if my application.exe is not the focused window ?
For example, like the Windows+D shortcut... Works everywhere...
I want that ALT+1 does a function, ALT+2 does another one, and so on...
You mean like this?
You need to use pyHook.
I have a pyHook example on my site. http://fadedbluesky.com/2011/using-pyhook-to-block-windows-key/
I originally wrote this to block keys in a game written using pygame. You should be able to easily adapt it to other windows programs.