PyQt通过点击按钮选择文件并显示相对路径的例子

PyQt通过点击按钮选择文件并显示相对路径的例子

import sys
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QFileDialog, QDialog, QApplication, QWidget,\
    QPushButton, QLabel, QLineEdit, QHBoxLayout, QFormLayout
from PyQt4.QtCore import QDir

class Form(QWidget):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        #self.le = QLabel()
        self.le = QLineEdit()
        self.le.setObjectName("Empty")
        self.le.setText("Empty")

        self.pb = QPushButton()
        self.pb.setObjectName("browse")
        self.pb.setText("Browse") 

        layout = QHBoxLayout()
        layout.addWidget(self.le)
        layout.addWidget(self.pb)

        self.setLayout(layout)
        self.connect(self.pb, SIGNAL("clicked()"),self.button_click)
        self.setWindowTitle("Learning")

    def button_click(self):
        # absolute_path is a QString object
        absolute_path = QFileDialog.getOpenFileName(self, 'Open file', 
            '.',"txt files (*.txt)")
        if absolute_path:
            cur_path = QDir('.')
            relative_path = cur_path.relativeFilePath(absolute_path)
            self.le.setText(relative_path)
        
            print relative_path


app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

你可能感兴趣的:(Python,Python,PyQt)