PYQT窗口居中

#UI.py,經過UI設計師製做後直接轉換爲UI.py腳本app

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

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)code

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

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

 

#Main.py,可視化UI.pyget

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

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

 

class MainWindow(QtGui.QMainWindow): class

 

    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_()

相關文章
相關標籤/搜索