import sys
import pymysql

from PyQt5.QtWidgets import QMainWindow, QApplication

from test import Ui_MainWindow

class Window(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(QMainWindow, self).__init__()
        self.setupUi(self) # 渲染页面控件
        self.connect_signals() # 设置信号槽

    def connect_signals(self):
        self.btn_sure.clicked.connect(self.btn_sure_clicked) # 绑定确定按钮事件
        self.btn_cancel.clicked.connect(self.btn_cancel_clicked) # 绑定取消按钮时间

    def btn_sure_clicked(self):
        self.show_edit.setText('hello world') # 点击确定按钮显示hello world

    def btn_cancel_clicked(self):
        self.show_edit.clear() # 点击取消按钮清空显示框

def dbPrint():
    dbHost = "127.0.0.1"
    dbPort = 3306
    dbUser = "root"
    dbPassword = "545715123"
    dbName = "mysql_demo"
    tableName = "tabel_demo"
    conn = pymysql.connect(host=dbHost  # 连接名称，默认127.0.0.1
                           , user=dbUser  # 用户名
                           , passwd=dbPassword  # 密码
                           , port=dbPort  # 端口，默认为3306
                           , db=dbName  # 数据库名称
                           )
    cur = conn.cursor()  # 生成游标对象
    sql = "select * from `" + tableName + "` "  # SQL语句
    cur.execute(sql)  # 执行SQL语句
    data = cur.fetchall()  # 通过fetchall方法获得数据
    for i in data:  # 打印输出所有数据
        print(i)
    cur.close()  # 关闭游标
    conn.close()  # 关闭连接

def main():
    dbPrint()
    app = QApplication(sys.argv)
    mywindow = Window()
    mywindow.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
    # dbPrint()