本文介绍了PyQt 不规则形状的窗口(例如,没有边框/装饰的圆形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 PyQt 中创建不规则形状的窗口?

How do I create an irregularly shaped window in PyQt?

我找到了这个 C++ 解决方案,但是我不确定如何在 Python 中做到这一点.

I found this C++ solution, however I am unsure of how to do that in Python.

推荐答案

给你:

from PyQt4 import QtGui, QtWebKit
from PyQt4.QtCore import Qt, QSize

class RoundWindow(QtWebKit.QWebView):
    def __init__(self):
        super(RoundWindow, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

    def sizeHint(self):
        return QSize(300,300)

    def paintEvent(self, event):
        qp = QtGui.QPainter()
        qp.begin(self)
        qp.setRenderHint(QtGui.QPainter.Antialiasing);
        qp.setPen(Qt.NoPen);
        qp.setBrush(QtGui.QColor(255, 0, 0, 127));
        qp.drawEllipse(0, 0, 300, 300);
        qp.end()

a = QtGui.QApplication([])
rw = RoundWindow()
rw.show()
a.exec_()

截图

我一生中从未编写过 C++,但阅读该代码示例并不难.您会发现大多数在线 Qt 文档都是用 C++ 编写的,因此至少能够阅读它很有用.

I've never written C++ in my life, but reading that code example was not that hard. You'll find that most Qt documentation online is in C++, so it's useful to at least be able to read.

这篇关于PyQt 不规则形状的窗口(例如,没有边框/装饰的圆形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 20:29