2

There is a button.
When it is clicked, file C:\file.txt should be opened with default text editor (as if it is double clicked).
Is it possible in pyQt? Button is pressed -> file is opened.
All I can google is just dialogs, but I don't need them.

file = 'C:\file.txt'
widget.connect(button, QtCore.SIGNAL('clicked()'), ????)

How it can be done?

Qiao
  • 16,565
  • 29
  • 90
  • 117
  • I think you're much farther from what you want than you probably expect. Do you have a PyQt GUI functioning and this is only a sample snippet, or is this your whole program? – Eli Bendersky May 18 '11 at 13:52

1 Answers1

4
def openFile(file):
    if sys.platform == 'linux2':
        subprocess.call(["xdg-open", file])
    else:
        os.startfile(file)

And edit your 2nd line to:

widget.connect(button, QtCore.SIGNAL('clicked()'), openFile(file))

Code for opening file copied from How to open a file with the standard application?

Community
  • 1
  • 1
Jacob
  • 41,721
  • 6
  • 79
  • 81
  • Why does file is started when I run script, no program (button) is showed? http://snipt.org/xJng . It is definitely has connection with `widget.connect` line – Qiao May 18 '11 at 14:06
  • 1
    & Qiao: the source line `connect(..., openFile(file))` _will call_ openFile. Use `lambda: openFile(file)` to connect to a function that calls openFile, not to whatever openFile() returns. :) – Macke May 18 '11 at 14:19
  • just started new question - http://stackoverflow.com/questions/6046362/pyqt-running-slot-from-the-start . I relay have no patience ) `lambda:` is working, but it is a black box for me now. – Qiao May 18 '11 at 14:31
  • @ Jacob : I tried your code but it is giving error as :NameError: global name 'subprocess' is not defined, please suggest – lkkkk Apr 03 '14 at 10:38
  • @lkkkk, please import subprocess before using it – West Jan 27 '16 at 06:00