pyqt5 .ui文件转换.py,并修改为自己的窗口

用QtDesigner设计界面后,保存为.ui文件,右键用PyUIC转换为.py文件,将UI_MAINWIDOW(或UI_FORM)改为自己的窗口类名(如myWindow),将

class Ui_Form(object):
    def setupUi(self, Form):

改为:

class myWin(QWidget):
    def __init__(self,parent=None):
        super(myWin, self).__init__(parent)
将原来的ui_mainwindow中MAINWINDOW(或FORM)改为self(ctrl+r)

然后可以添加封装的类,可以重写一些方法,如改写QLabel的mousePressEvent:

class myLabel(QLabel):
    def __init__(self,parent=None):
        super(myLabel,self).__init__(parent)
    def mousePressEvent(self,e):
        filename,_=QFileDialog.getOpenFileName(self,'','','')
        self.setPixmap(QPixmap(filename))
label=myLabel()

可以实现鼠响应标点击事件

猜你喜欢

转载自blog.csdn.net/qq_26669719/article/details/80506393