[python] Pyside2 and database read data display linkage

from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
import pymysql

class Windows:
    def __init__(self):
        self.ui = QUiLoader().load('show_news.ui')#载入ui文件
        self.ui.show_button.clicked.connect(self.show1)#fun_show1为按钮show_button调用的函数
        self.ui.show()

    def fun_show1(self):
        SQL = "SELECT * FROM out_news order by 'date'"
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='xjsql', charset='UTF8')
        cursor = conn.cursor()
        cursor.execute(SQL)
        count = 1
        temp = ""
        for i in cursor.fetchall():
            temp += str(count) + "." + i[0] + "\n"
            count += 1
        self.ui.textBrowser.setPlainText(temp)
        conn.close()

alla = QApplication([])
windows = Windows()
alla.exec_()

Guess you like

Origin blog.csdn.net/Sgmple/article/details/112738770