pyqt改变背景颜色
首先是界面的布置,代码如下所示,使用的是Designer。
-
# -*- coding: utf-8 -*-
-
# Form implementation generated from reading ui file 'untitled.ui'
-
#
-
# Created by: PyQt5 UI code generator 5.6
-
#
-
# WARNING! All changes made in this file will be lost!
-
from PyQt5 import QtCore, QtGui, QtWidgets
-
class Ui_MainWindow(object):
-
def setupUi(self, MainWindow):
-
MainWindow.setObjectName("MainWindow")
-
MainWindow.resize(800, 600)
-
self.centralwidget = QtWidgets.QWidget(MainWindow)
-
self.centralwidget.setObjectName("centralwidget")
-
self.btn_1 = QtWidgets.QPushButton(self.centralwidget)
-
self.btn_1.setGeometry(QtCore.QRect(220, 191, 75, 23))
-
self.btn_1.setObjectName("btn_1")
-
self.btn_2 = QtWidgets.QPushButton(self.centralwidget)
-
self.btn_2.setGeometry(QtCore.QRect(220, 220, 75, 23))
-
self.btn_2.setObjectName("btn_2")
-
self.btn_3 = QtWidgets.QPushButton(self.centralwidget)
-
self.btn_3.setGeometry(QtCore.QRect(220, 249, 75, 23))
-
self.btn_3.setObjectName("btn_3")
-
self.btn_4 = QtWidgets.QPushButton(self.centralwidget)
-
self.btn_4.setGeometry(QtCore.QRect(220, 278, 75, 23))
-
self.btn_4.setObjectName("btn_4")
-
self.label = QtWidgets.QLabel(self.centralwidget)
-
self.label.setGeometry(QtCore.QRect(310, 190, 71, 101))
-
font = QtGui.QFont()
-
font.setFamily("Arial")
-
font.setPointSize(36)
-
self.label.setFont(font)
-
self.label.setAlignment(QtCore.Qt.AlignCenter)
-
self.label.setObjectName("label")
-
self.btn_exit = QtWidgets.QPushButton(self.centralwidget)
-
self.btn_exit.setGeometry(QtCore.QRect(220, 330, 75, 23))
-
self.btn_exit.setObjectName("btn_exit")
-
MainWindow.setCentralWidget(self.centralwidget)
-
self.menubar = QtWidgets.QMenuBar(MainWindow)
-
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
-
self.menubar.setObjectName("menubar")
-
MainWindow.setMenuBar(self.menubar)
-
self.statusbar = QtWidgets.QStatusBar(MainWindow)
-
self.statusbar.setObjectName("statusbar")
-
MainWindow.setStatusBar(self.statusbar)
-
self.retranslateUi(MainWindow)
-
QtCore.QMetaObject.connectSlotsByName(MainWindow)
-
def retranslateUi(self, MainWindow):
-
_translate = QtCore.QCoreApplication.translate
-
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
-
self.btn_1.setText(_translate("MainWindow", "1"))
-
self.btn_2.setText(_translate("MainWindow", "2"))
-
self.btn_3.setText(_translate("MainWindow", "3"))
-
self.btn_4.setText(_translate("MainWindow", "4"))
-
self.label.setText(_translate("MainWindow", "0"))
-
self.btn_exit.setText(_translate("MainWindow", "退出"))
接下来是运行代码,如下所示:
-
# -*- coding: utf-8 -*-
-
'''
-
通过按钮改变Label的背景颜色
-
'''
-
import os
-
import sys
-
import time
-
from tqdm import tqdm
-
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QMainWindow
-
from PyQt5.QtGui import QPalette
-
from PyQt5.QtCore import Qt
-
from application.NewUi.untitled import Ui_MainWindow
-
class my_window(Ui_MainWindow, QMainWindow):
-
def __init__(self):
-
super(my_window, self).__init__()
-
self.setupUi(self)
-
self.label.setAutoFillBackground(True)
-
global palette_red, palette_green, palette_blue, palette_yellow
-
palette_red = QPalette()
-
palette_green = QPalette()
-
palette_blue = QPalette()
-
palette_yellow = QPalette()
-
palette_red.setColor(QPalette.Window, Qt.red)
-
palette_green.setColor(QPalette.Window, Qt.green)
-
palette_blue.setColor(QPalette.Window, Qt.blue)
-
palette_yellow.setColor(QPalette.Window, Qt.yellow)
-
self.btn_1.clicked.connect(self.btn_1_clicked)
-
self.btn_2.clicked.connect(self.btn_2_clicked)
-
self.btn_3.clicked.connect(self.btn_3_clicked)
-
self.btn_4.clicked.connect(self.btn_4_clicked)
-
self.btn_exit.clicked.connect(self.exit_systerm)
-
self.setWindowTitle('通过按钮改变Label的背景颜色')
-
def btn_1_clicked(self):
-
self.label.setPalette(palette_red)
-
def btn_2_clicked(self):
-
self.label.setPalette(palette_blue)
-
def btn_3_clicked(self):
-
self.label.setPalette(palette_green)
-
def btn_4_clicked(self):
-
self.label.setPalette(palette_yellow)
-
def exit_systerm(self):
-
self.close()
-
def closeEvent(self, event):
-
reply = QMessageBox.question(self, 'Exit', 'Are you sure to quit?', QMessageBox.Yes | QMessageBox.Cancel)
-
if reply == QMessageBox.Yes:
-
event.accept()
-
else:
-
event.ignore()
-
if __name__ == '__main__':
-
app = QApplication(sys.argv)
-
w = my_window()
-
w.show()
-
sys.exit(app.exec_())
特别注意:一定要在想要变色的控件下面加上这句话self.label.setAutoFillBackground(True)
其中标红的地方是你控件的id,如果不加这句话是不能变色的。
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/108327378
- 点赞
- 收藏
- 关注作者
评论(0)