要将PyQt界面做得像网页一样漂亮,可以从以下几个方面入手:
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QVBoxLayout
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
button = QPushButton('Styled Button')
button.setStyleSheet("""
QPushButton {
background-color: #4CAF50;
color: white;
font-size: 16px;
border-radius: 10px;
padding: 10px 20px;
}
QPushButton:hover {
background-color: #45a049;
}
""")
layout.addWidget(button)
window.setLayout(layout)
window.show()
app.exec_()
from PyQt5.QtWidgets import QLabel
from PyQt5.QtGui import QPainter, QColor, QBrush
from PyQt5.QtCore import Qt
class CustomLabel(QLabel):
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(QBrush(QColor(100, 150, 200)))
painter.drawRoundedRect(self.rect(), 10, 10)
super().paintEvent(event)
label = CustomLabel('Custom Label')
label.setAlignment(Qt.AlignCenter)
label.setStyleSheet("color: white; font-size: 18px;")
layout.addWidget(label)
from PyQt5.QtCore import QPropertyAnimation, QRect
animation = QPropertyAnimation(button, b"geometry")
animation.setDuration(1000)
animation.setStartValue(button.geometry())
animation.setEndValue(QRect(100, 100, 200, 50))
animation.start()
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
web_view = QWebEngineView()
web_view.setUrl(QUrl("https://www.example.com"))
layout.addWidget(web_view)
from qtmodern.windows import ModernWindow
from qtmodern.styles import dark
modern_window = ModernWindow(window)
dark(app)
modern_window.show()
from PyQt5.QtGui import QFontDatabase
font_id = QFontDatabase.addApplicationFont("path/to/font.ttf")
font_name = QFontDatabase.applicationFontFamilies(font_id)[0]
button.setFont(QFont(font_name, 16))
总结
通过QSS、自定义控件、动画、Web技术、第三方库、合理布局和图标字体,你可以将PyQt界面设计得像网页一样美观。