我用PyQT5设计器工具创建了一个带有单个按钮的简单窗口GuiPy代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ela.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 350, 111, 31))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("QPushButton { n"
"background-color: rgb(100, 176, 176);n"
"border: 5px solid rgb(100, 150, 160);n"
"border-radius: 10px;n"
"/*color:white*/n"
"}n"
"QPushButton:hover{n"
"/*background-color: rgb(180, 230, 240);*/n"
"border: 5px solid rgb(0, 0, 255);n"
"}")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 22))
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.pushButton.setText(_translate("MainWindow", "01"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我想当我打开窗口我的应用程序从我的服务器读取数据库,并根据阅读(它将是0或1或2作为输入)改变按钮的背景颜色。例如,if在表中第3列的最后一行中读取"0";背景色变为绿色,如果显示为"1"更改为红色
我添加一个类到前面的代码,以连接和读取我的数据库:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ela.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import mysql.connector
class ReadDataBase:
def __init__(self, Table):
self.Table = Table
pos01 = ""
pos02 = ""
def connecting(self):
mydb = mysql.connector.connect(
host="192.168.1.110", # working ok
user="user",
password="pass",
database="Databs"
)
mycursor = mydb.cursor()
# select last row (all colums) from the table
mycursor.execute("SELECT * FROM" + " " + self.Table + " " +"ORDER BY id DESC LIMIT 1")
myresult = mycursor.fetchone()
# prints the result of the database but it is as a tuple ,
print(myresult)
# check and store every possition on the tuple
self.pos01 = myresult[2]
# only if we want to return this parameter and not the others after this command
#return self.pos01
self.pos02 = myresult[3]
# returns all the parameters
return self.pos02, self.pos01
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 350, 111, 31))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("QPushButton { n"
"background-color: rgb(100, 176, 176);n"
"border: 5px solid rgb(100, 150, 160);n"
"border-radius: 10px;n"
"/*color:white*/n"
"}n"
"QPushButton:hover{n"
"/*background-color: rgb(180, 230, 240);*/n"
"border: 5px solid rgb(0, 0, 255);n"
"}")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 22))
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.pushButton.setText(_translate("MainWindow", "01"))
def checkDatabase():
ant = ""
#ant2 = ""
test = ReadDataBase("YF10")
test.connecting()
if test.pos01 == 0:
ant = test.pos01
return ant
if test.pos01 != 0:
ant = test.pos01
return ant
# later button to add
#if test.pos02 == 0:
# ant2 = test.pos02
#if test.pos02 != 0:
# ant2 = test.pos02
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Ui_MainWindow.checkDatabase()
sys.exit(app.exec_())
好的,它工作…当我运行脚本时,我有一个带有按钮的窗口,并且在终端中,我可以从我的最后一个数据库条目中获得读数:**(19,datetime)。Datetime (2023,3,27,15,34,24), 0,1,0,0,0,0) **哪里的"19";是第一列条目id,下一列是日期,其他是我想要获取按钮读数的内容
作为编程python和OOP逻辑的新用户,我想帮助我如何通过我的"checkDatabase()"方法读取到主类,所以我可以改变我的PushButton的颜色
thanks in advance
我尝试了多种方法将数据库读数传递给主类,但没有结果(newbie)
要根据数据库读取改变按钮的背景颜色,需要修改Ui_MainWindow类的setupUi方法,使其使用ReadDataBase类读取数据库,并相应地设置按钮的背景颜色。您可以通过向setupUi方法添加以下代码来实现这一点:
db = ReadDataBase("your_table_name")
pos1, pos2 = db.connecting()
if pos2 == "0":
self.pushButton.setStyleSheet("background-color: green;")
elif pos2 == "1":
self.pushButton.setStyleSheet("background-color: red;")
这段代码首先用要读取的表的名称创建一个ReadDataBase类的实例。然后调用连接方法读取表的最后一行,并将值存储在pos1和pos2变量中。最后,它检查pos2的值,并相应地设置按钮的背景颜色。