在 MacOS 上使用 VSCode。我试图在 Python 中使用 Kivy 创建一个现代的启动屏幕。这是我的代码:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, RoundedRectangle
from kivy.core.window import Window
class RoundedWindow(FloatLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
with self.canvas.before:
Color(0.6, 0.4, 0.8, 1)
self.rect = RoundedRectangle(size = self.size, pos = self.pos, radius = [36])
self.bind(size = self.update_rect, pos=self.update_rect)
def update_rect(self, instance, value):
self.rect.pos = self.pos
self.rect.size = self.size
class RoundedWindowApp(App):
def build(self):
Window.borderless = True
Window.size = (360, 360)
return RoundedWindow()
if __name__ == '__main__':
RoundedWindowApp().run()
运行时,Kivy 窗口的标题栏被删除,但尝试使新窗口具有角半径失败。它显示了这个:
如何去除黑角?
好吧,显然这种事情不值得或者根本不可能。我找到了一种在 PyQt5 上复制这个的方法。
import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPainter, QBrush, QColor, QFont
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
class RoundedWindow(QWidget):
def __init__(self):
super().__init__()
# Set up the window properties
self.setWindowFlags(Qt.FramelessWindowHint) # Remove title bar
self.setAttribute(Qt.WA_TranslucentBackground) # Enable transparency
self.resize(400, 300) # Set initial window size
# Center the window on the screen
self.center_window()
# Add the "Swift client" label
label = QLabel('Swift Client', self)
label.setAlignment(Qt.AlignCenter)
label.setStyleSheet("color: white; font-size: 24px;")
label.setFont(QFont("eiuroeuore", 20, QFont.Bold))
# Use a vertical box layout to center the label
layout = QVBoxLayout(self)
layout.addWidget(label)
self.setLayout(layout)
def center_window(self):
frame_geometry = self.frameGeometry()
screen_center = QApplication.desktop().screenGeometry().center()
frame_geometry.moveCenter(screen_center)
self.move(frame_geometry.topLeft())
def paintEvent(self, event):
# Set rounded corners and a gradient background
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
rounded_rect = self.rect()
color = QColor(40, 44, 52) # Dark background color
brush = QBrush(color)
painter.setBrush(brush)
painter.setPen(Qt.NoPen)
painter.drawRoundedRect(rounded_rect, 30, 30) # 30 is the radius for rounded corners
if __name__ == "__main__":
app = QApplication(sys.argv)
window = RoundedWindow()
window.show()
sys.exit(app.exec_())