vss
vss

Reputation: 607

Can I use something other than 'self' in this PyQt Function?

I am new to Python and PyQt. This function is not within a class, and is called by another function that is not self referencing. (I'm not sure if that's what you call it. What I mean is, none of the functions are of the type function_name(self))

def openFileDialog():
    filename = QtGui.QFileDialog.getOpenFileName(self, "Open File", "/home/username/Pictures")
    print(filename)

What can I pass instead of self? I have tried Dialog, none, parent=none, but neither of those work.

Upvotes: 1

Views: 1022

Answers (1)

Brendan Abel
Brendan Abel

Reputation: 37599

The parent argument is just so the QFileDialog is correctly parented to a widget. There are many reasons why you would want dialogs and windows to be correctly parented to each other.

  • They inherit the styling and color palette of the parent.
  • They will be created with a window position relative to the parent.
  • The window manager for your OS will treat them as the same application (for example, on a taskbar or dock, both dialogs/windows will be grouped together).
  • Unhandled events propagate up to the parent.

If you don't care about any of these things, you could just pass in None.

filename = QtGui.QFileDialog.getOpenFileName(None, "Open File", "/home/username/Pictures")

Or, to be a little more versatile, give your function an optional parent argument

def openFileDialog(parent=None):
    filename = QtGui.QFileDialog.getOpenFileName(parent, "Open File", "/home/username/Pictures")
    print(filename)

Upvotes: 2

Related Questions