使用Pyqt5创建一个mysql数据库登录界面

时间:2018-04-20 18:19:03

标签: python pyqt5 mysql-python

人 我正在学习如何编码,我试图创建一个pyqt5登录界面,潜在用户可以输入主机,用户,密码和daabase并登录到mysql数据库 很简单,除了我是一个想要将职业转向编码的新手,所以无论多么努力,我似乎​​都无法将输入信息输入我的.connect()。 我现在已经尝试了3个徒劳的日子,所以如果有人可以帮助我,我会感激不尽 这是我的代码

import sys
from PyQt5.QtWidgets import *
import MySQLdb as mdb


class App(QWidget):


def __init__(self):
    super().__init__()
    self.title = 'Login'
    self.top = 100
    self.left = 100
    self.width = 400
    self.height = 400
    self.Log()



def Log(self):
    self.setWindowTitle(self.title)
    self.setGeometry(self.top, self.left, self.height, self.width)

    self.host = QLineEdit(self)
    self.host.setPlaceholderText('Host')
    self.host.move(100, 100)
    self.user = QLineEdit(self)
    self.user.setPlaceholderText('User Name')
    self.user.move(100, 150)
    self.pw = QLineEdit(self)
    self.pw.setPlaceholderText('Password')
    self.pw.move(100, 200)
    self.db = QLineEdit(self)
    self.db.setPlaceholderText('Database')
    self.db.move(100, 250)

    btn = QPushButton('Click to login', self)
    btn.move(100, 300)
    btn.clicked.connect(self.SQL)

    self.show()

def SQL(self):
    try:
       conn = mdb.connect(suppsed to tak input from above here)
       QMessageBox.about(self, 'Connection', 'Done')
    except:
        QMessageBox.about(self, 'Connection', 'Failed')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:0)

使用Qt时,习惯于解析C ++文档非常有用。 QLineEdit is here的页面。

您可以使用以下代码从QLineEdit小部件获取输入字符串:

# Gets username and password from your widgets
input_user = self.user.text()
input_password = self.pw.text()

顺便说一句,既然你刚接触编码,你应该避免这样做:

from PyQt5.QtWidgets import *

它会污染命名空间,并可能难以跟踪错误。此外,python中函数/方法名称的约定是snake_case

答案 1 :(得分:0)

试一试:

import sys
from PyQt5.QtWidgets import *
import MySQLdb as mdb


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'Login'
        self.top = 100
        self.left = 100
        self.width = 400
        self.height = 400
        self.Log()



    def Log(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.height, self.width)

        self.host = QLineEdit(self)
        self.host.setPlaceholderText('Host')
        self.host.move(100, 100)
        self.user = QLineEdit(self)
        self.user.setPlaceholderText('User Name')
        self.user.move(100, 150)
        self.pw = QLineEdit(self)
        self.pw.setPlaceholderText('Password')
        self.pw.move(100, 200)
        self.db = QLineEdit(self)
        self.db.setPlaceholderText('Database')
        self.db.move(100, 250)

        btn = QPushButton('Click to login', self)
        btn.move(100, 300)
        btn.clicked.connect(self.SQL)

        self.show()

    def SQL(self):
        try:
            #conn = mdb.connect(suppsed to tak input from above here)
            # !!! indicate your names                 vvvv           vvvvvv      vvvvvv
            conn = mdb.connect(host="localhost",user="user", passwd="passwd",db="testdb")
            with conn:
                cur = conn.cursor()
                """
                cur.execute('''INSERT INTO pyqt5data(host, user, pw, ...)
                               VALUES (%s, %s, %s)''', 
                               ( self.host.text(),
                                 self.user.text(),
                                 self.pw.text() 
                                 ...
                               )
                           )
                """
                cur.close() 

            QMessageBox.about(self, 'Connection', 
                                    'Done \nconn-{} \ncur- {}'.format(conn, cur))


        except:
            QMessageBox.about(self, 'Connection', 'Failed')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

enter image description here