pyqt4 borderless window

coding:utf-8

import sys
from PyQt4.QtGui import (QApplication, QWidget, QPushButton, QLineEdit, QHBoxLayout, QVBoxLayout, QColorDialog, QInputDialog, QFileDialog, QCursor, QColor)

from PyQt4.QtCore import Qt

class Example(QWidget):
def init(self):
super(Example, self).init()
self.style = """
QPushButton{background-color:grey;color:white;}
#window{ background:pink; }
#test{ background-color:black;color:white; }
"""
self.setStyleSheet(self.style)
self.setWindowFlags(Qt.FramelessWindowHint)
self.initUI()

def initUI(self):
    self.setGeometry(300, 300, 300, 220)
    self.setWindowTitle('test')
    self.setObjectName("window")

    btn1 = QPushButton(u"关闭", self)
    btn1.clicked.connect(self.close)
    btn1.setObjectName('test')
    btn2 = QPushButton(u"最小化", self)
    btn2.clicked.connect(self.showMinimized)
    btn3 = QPushButton(u"最大化", self)
    btn3.clicked.connect(self.showMaximized)

    btn11 = QPushButton(u"改变背景", self)
    btn11.clicked.connect(self.showColor)
    btn22 = QPushButton(u"选择文件", self)
    btn22.clicked.connect(self.showFile)
    btn33 = QPushButton(u"对话框", self)
    btn33.clicked.connect(self.showDialog)
    self.linet1 = QLineEdit("111111", self)
    self.linet2 = QLineEdit("ssssssss", self)

    hbox1 = QHBoxLayout()  # 水平布局
    hbox1.addWidget(btn1)
    hbox1.addWidget(btn2)
    hbox1.addWidget(btn3)
    hbox2 = QHBoxLayout()  # 水平布局
    hbox2.addWidget(btn11)
    hbox2.addWidget(btn22)
    hbox2.addWidget(btn33)
    vbox = QVBoxLayout()  # 垂直布局
    vbox.addLayout(hbox1)
    vbox.addLayout(hbox2)
    vbox.addWidget(self.linet1)
    vbox.addWidget(self.linet2)
    self.setLayout(vbox)

    self.show()

def mousePressEvent(self, event):
    if event.button() == Qt.LeftButton:
        self.m_drag = True
        self.m_DragPosition = event.globalPos() - self.pos()
        event.accept()
        self.setCursor(QCursor(Qt.OpenHandCursor))

def mouseMoveEvent(self, QMouseEvent):
    if Qt.LeftButton and self.m_drag:
        self.move(QMouseEvent.globalPos() - self.m_DragPosition)
        QMouseEvent.accept()

def mouseReleaseEvent(self, QMouseEvent):
    self.m_drag = False
    self.setCursor(QCursor(Qt.ArrowCursor))

def showColor(self):
    col = QColorDialog.getColor()
    if col.isValid():
        self.setStyleSheet(self.style + "#window{background:%s}" % col.name())

def showDialog(self):
    text, ok = QInputDialog.getText(self, u'对话框',
                                    u'请输入你的名字:')

    if ok:
        self.linet1.setText(str(text))

def showFile(self):
    fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')

    if fname[0]:
        f = open(fname[0], 'r')

        with f:
            data = f.readline()
            self.linet1.setText(data)

if name == 'main':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

posted @ 2017-02-10 00:42  idlewith  阅读(316)  评论(0)    收藏  举报