zoukankan      html  css  js  c++  java
  • 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_())

  • 相关阅读:
    澄净是什么意思?
    【Cavali风格/优质羊毛混纺面料/高密抗静电里衬/撞色拼皮/立领/绿色/便装单西】玛萨玛索男装网购商城
    【100%纯新美利奴羊毛(除装饰材料外)/半高领/丈青/商务毛衫】玛萨玛索男装网购商城
    victim是什么意思_victim在线翻译_英语_读音_用法_例句_海词词典
    Lind.DDD.Repositories.EF层介绍
    Lind.DDD.Domain领域模型介绍
    大叔也说Xamarin~Android篇~原生登陆与WebView的网站如何共享Session
    Redis学习笔记~Redis并发锁机制
    知方可补不足~sqlserver中对xml类型字段的操作
    json转String 和 String转json 和判断对象类型
  • 原文地址:https://www.cnblogs.com/otfsenter/p/6384750.html
Copyright © 2011-2022 走看看