我正在设计一个 GUI,作为 GUI 的一部分,我想以一种特殊的方式检索数据并将其显示在滚动区域上。但是我不知道该怎么做,我已将数据库检索功能定义为 cpu_alert,但是如何将结果(行)放入滚动区域?请看一下代码。
from PyQt4 import QtCore, QtGui
import ui_intex_server_monitorui as intex
from PyQt4 import Qt
import sys
import os
import MySQLdb as mdb
class MyMain(QtGui.QMainWindow, intex.Ui_MainWindow):
def __init__(self, parent=None):
super(MyMain, self).__init__(parent)
self.setupUi(self)
#Exit Button
self.connect(self.pushButton_2, QtCore.SIGNAL('clicked()'),
self.close)
#Refresh Button here#
self.testdata = QtGui.QListWidget(self)
layout = self.scrollArea_10
layout.setWidget(self.testdata)
self.testdata.addItem(self.cpu_alert.rows)
def cpu_alert():
control = True
while (control == True):
f = open("connection_cpu.txt","r")
a = f.read()
con = mdb.connect('server', 'user', 'pass', 'db')
if a == "CPU Overload":
with con:
cur = con.cursor()
cur.execute("""SELECT *
FROM cpu_alert
WHERE date = (SELECT MAX(date) FROM cpu_alert)""")
rows = str(cur.fetchone())
rows = rows.strip("()" "''")
open("connection_cpu.txt","w").close()
return rows
def main():
app = QtGui.QApplication(sys.argv)
app.setStyle(QtGui.QStyleFactory.create("plastique"))
main_window = MyMain()
main_window.show()
app.exec_()
if __name__ == '__main__':
main()