Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file with pyQt

Tags:

python

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?

like image 435
Qiao Avatar asked Dec 11 '25 18:12

Qiao


2 Answers

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?

like image 125
Jacob Avatar answered Dec 14 '25 08:12

Jacob


use this method with lambda and easily open any file you want

self.ui.pushButton.clicked.connect(lambda:os.startfile(".\help.png"))
like image 30
We_Are_All_Same Avatar answered Dec 14 '25 08:12

We_Are_All_Same