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