用pyqt5写数据库管理界面
时间: 2023-09-24 11:06:19 浏览: 94
下面是一个简单的Python PyQt5程序,用于管理数据库:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout
from PyQt5.QtGui import QIcon
import sqlite3
class DatabaseManager(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Database Manager')
self.setGeometry(100, 100, 400, 300)
self.setWindowIcon(QIcon('icon.png'))
vbox = QVBoxLayout()
label_username = QLabel('Username:')
self.edit_username = QLineEdit()
vbox.addWidget(label_username)
vbox.addWidget(self.edit_username)
label_password = QLabel('Password:')
self.edit_password = QLineEdit()
self.edit_password.setEchoMode(QLineEdit.Password)
vbox.addWidget(label_password)
vbox.addWidget(self.edit_password)
button_login = QPushButton('Login')
button_login.clicked.connect(self.login)
vbox.addWidget(button_login)
self.setLayout(vbox)
def login(self):
username = self.edit_username.text()
password = self.edit_password.text()
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM users WHERE username=? AND password=?', (username, password))
result = cursor.fetchone()
conn.close()
if result:
print('Login successful')
else:
print('Invalid username or password')
if __name__ == '__main__':
app = QApplication(sys.argv)
db_manager = DatabaseManager()
db_manager.show()
sys.exit(app.exec_())
```
该程序包含一个窗口,该窗口有两个标签和两个文本框,用于输入用户名和密码,以及一个按钮,用于登录到数据库。当用户单击登录按钮时,程序将连接到SQLite数据库并验证用户名和密码。如果用户名和密码是正确的,则程序将打印“登录成功”消息,否则将打印“无效的用户名或密码”消息。该程序还包括一个图标,该图标将显示在窗口的标题栏和任务栏中。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)