python - 我在组织 PyQt5 Gui 代码时遇到问题
问题描述
我正在编写一个 Pyqt5 GUI 代码,但我在组织我的代码时遇到了问题,尤其是通过 setStyle,因为样式太长了我该怎么办?任何建议示例
setStyleSheet("QPushButton{ color: #b1b1b1;"
" background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #565656, stop: 0.1 #525252, stop: 0.5 #4e4e4e, stop: 0.9 #4a4a4a, stop: 1 #464646);"
" border-width: 1px;"
" border-color: #1e1e1e;"
" border-style: solid;"
" border-radius: 6;"
"padding: 3px;"
"font-size: 20px;"
" padding-left: 5px;"
"padding-right: 5px;"
"min-width: 40px;"
"} QPushButton::hover"
"{"
"background-color : #444444; color : green"
"}"
" QPushButton:pressed"
"{"
"background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop:
#2d2d2d, stop: 0.1 #2b2b2b, stop: 0.5 #292929, stop: 0.9 #282828, stop: 1 #252525);"
"}"
";")
解决方案
Qt 样式表基于 CSS 2.1,因此您可以使用以下格式:
样式.css
QPushButton {
color: #b1b1b1;
background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #565656, stop: 0.1 #525252, stop: 0.5 #4e4e4e, stop: 0.9 #4a4a4a, stop: 1 #464646);
border-width: 1px;
border-color: #1e1e1e;
border-style: solid;
border-radius: 6;
padding: 3px;
font-size: 20px;
padding-left: 5px;
padding-right: 5px;
min-width: 40px;
}
QPushButton::hover {
background-color: #444444;
color: green
}
QPushButton:pressed {
background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: #2d2d2d, stop: 0.1 #2b2b2b, stop: 0.5 #292929, stop: 0.9 #282828, stop: 1 #252525);
}
* .py
with open("style.css", "r") as f:
app.setStyleSheet(f.read())
推荐阅读
- react-native - 如何将 HTML 转换为 pdf 并下载到外部存储 (Android&IOS) react-native?
- javascript - 无法在 Jest/Enzyme 中呈现可切换组件
- oracle - 如何在 Oracle SQL Developer 中一次连接到具有所有已保存连接的数据库
- azure - 使用 docker compose 将多个容器部署到 azure
- angular - 我的 data.json 有一个链接,我想在我的视图中访问该链接如何在不使用 angular 中的 innerhtml 的情况下访问该链接
- c# - 当其他按钮处于活动状态时,按钮/更改边框颜色 C# 设置回默认值
- cdi - 使用 CDI 2.0 注入实体管理器时抛出 ClassNotFoundException
- html - mailchimp 电子邮件在 gmail 中未正确显示
- vscode-settings - VS Code 终端 Java 隐藏路径
- c++ - CoInitializeSecurity() 和媒体基础编码器之间的奇怪连接