PYQT設計無邊框窗體

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

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

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

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

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

 

#Main.py,可視化UI.pyorm

# -*- coding: utf-8 -*-事件

from PyQt4 import QtCore, QtGui, Qt
from UI import *utf-8

 

class MainWindow(QtGui.QMainWindow): get

 

    def __init__(self,parent=None):

 

        QtGui.QWidget.__init__(self,parent)
        self.ui=Ui_Form()
        self.ui.setupUi(self)
        self.setWindowFlags(Qt.Qt.FramelessWindowHint)
        #窗口無邊框化,寫於QMainwindow所在子類初始化函數中

        #或者self.setWindowFlags(Qt.Qt.SplashScreen)

        self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)

        #窗口背景透明

 

    def mousePressEvent(self,event):

 

       #定義鼠標點擊事件
       if event.button() == QtCore.Qt.LeftButton:
           self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
           event.accept()

 

    def mouseMoveEvent(self,event):

 

       #定義鼠標移動事件
        if event.buttons() ==QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()

 

    def resizeEvent(self,event):

 

       #重繪窗體背景
        image=QtGui.QImage()
        image.load("images/background.png")
        palette=QtGui.QPalette()
        palette.setBrush(QtGui.QPalette.Window,QtGui.QBrush(image.scaled(event.size(),Qt.Qt.KeepAspectRatioByExpanding,Qt.Qt.SmoothTransformation)))
        self.setPalette(palette)

 

if __name__ == "__main__":

 

    import sys

 

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

相關文章
相關標籤/搜索