PYQT窗口居中

#UI.py,通过UI设计师制作后直接转换为UI.py脚本

# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))

#Main.py,可视化UI.py

# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui, Qt
from UI import *

扫描二维码关注公众号,回复: 6747808 查看本文章

class MainWindow(QtGui.QMainWindow): 

    def __init__(self,parent=None):

        QtGui.QWidget.__init__(self,parent)
        self.ui=Ui_Form()
        self.ui.setupUi(self)

        #窗口居中显示
        desktop =QtGui.QApplication.desktop()
        width = desktop.width()
        height = desktop.height()
        self.move((width - self.width())/2, (height - self.height())/2)
        self.setMouseTracking(True)

if __name__ == "__main__":

    import sys

    app = QtGui.QApplication(sys.argv)
    myapp=MainWindow()
    myapp.show()
    app.exec_()

转载于:https://www.cnblogs.com/doudongchun/p/3694798.html

猜你喜欢

转载自blog.csdn.net/weixin_33709609/article/details/93959294