简体   繁体   中英

Open file with pyQt

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM