python连接sqlite数据库界面设计
时间: 2023-04-21 21:06:25 浏览: 336
Python连接SQLite数据库界面设计可以使用PyQt或Tkinter等GUI库来实现。以下是一个简单的示例:
1. 使用PyQt实现界面设计
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout
import sqlite3
class SQLiteGUI(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.db_label = QLabel('Database Name:')
self.db_edit = QLineEdit()
self.table_label = QLabel('Table Name:')
self.table_edit = QLineEdit()
self.query_button = QPushButton('Query')
self.result_label = QLabel('Result:')
vbox = QVBoxLayout()
vbox.addWidget(self.db_label)
vbox.addWidget(self.db_edit)
vbox.addWidget(self.table_label)
vbox.addWidget(self.table_edit)
vbox.addWidget(self.query_button)
vbox.addWidget(self.result_label)
self.setLayout(vbox)
self.query_button.clicked.connect(self.query)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('SQLite GUI')
self.show()
def query(self):
db_name = self.db_edit.text()
table_name = self.table_edit.text()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {table_name}")
result = cursor.fetchall()
self.result_label.setText(str(result))
cursor.close()
conn.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = SQLiteGUI()
sys.exit(app.exec_())
```
2. 使用Tkinter实现界面设计
```python
import tkinter as tk
import sqlite3
class SQLiteGUI:
def __init__(self, master):
self.master = master
master.title('SQLite GUI')
self.db_label = tk.Label(master, text='Database Name:')
self.db_edit = tk.Entry(master)
self.table_label = tk.Label(master, text='Table Name:')
self.table_edit = tk.Entry(master)
self.query_button = tk.Button(master, text='Query', command=self.query)
self.result_label = tk.Label(master, text='Result:')
self.db_label.pack()
self.db_edit.pack()
self.table_label.pack()
self.table_edit.pack()
self.query_button.pack()
self.result_label.pack()
def query(self):
db_name = self.db_edit.get()
table_name = self.table_edit.get()
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {table_name}")
result = cursor.fetchall()
self.result_label.config(text=str(result))
cursor.close()
conn.close()
if __name__ == '__main__':
root = tk.Tk()
ex = SQLiteGUI(root)
root.mainloop()
```
以上是两种常用的Python连接SQLite数据库界面设计方法,可以根据实际需求进行修改和扩展。
阅读全文