用python生产一个ui界面用来查看mysql数据库
时间: 2023-06-16 15:02:59 浏览: 142
可以使用Python的Tkinter库来创建一个简单的UI界面,连接MySQL数据库并显示数据。以下是一个基本的代码示例:
```python
import tkinter as tk
import mysql.connector
from mysql.connector import Error
def connect():
try:
global conn
conn = mysql.connector.connect(host=host_entry.get(),
database=db_entry.get(),
user=user_entry.get(),
password=password_entry.get())
if conn.is_connected():
status_label.config(text="Connected")
except Error as e:
status_label.config(text="Error: " + str(e))
def show_data():
try:
cursor = conn.cursor()
cursor.execute("SELECT * FROM " + table_entry.get())
data = cursor.fetchall()
for idx, row in enumerate(data):
for col in range(len(row)):
tk.Label(data_frame, text=row[col], width=15).grid(row=idx, column=col)
except Error as e:
status_label.config(text="Error: " + str(e))
window = tk.Tk()
window.title("MySQL Viewer")
host_label = tk.Label(window, text="Host:")
host_label.grid(row=0, column=0)
host_entry = tk.Entry(window)
host_entry.grid(row=0, column=1)
db_label = tk.Label(window, text="Database:")
db_label.grid(row=1, column=0)
db_entry = tk.Entry(window)
db_entry.grid(row=1, column=1)
user_label = tk.Label(window, text="User:")
user_label.grid(row=2, column=0)
user_entry = tk.Entry(window)
user_entry.grid(row=2, column=1)
password_label = tk.Label(window, text="Password:")
password_label.grid(row=3, column=0)
password_entry = tk.Entry(window, show="*")
password_entry.grid(row=3, column=1)
connect_button = tk.Button(window, text="Connect", command=connect)
connect_button.grid(row=4, column=0)
table_label = tk.Label(window, text="Table:")
table_label.grid(row=5, column=0)
table_entry = tk.Entry(window)
table_entry.grid(row=5, column=1)
show_button = tk.Button(window, text="Show Data", command=show_data)
show_button.grid(row=6, column=0)
status_label = tk.Label(window, text="")
status_label.grid(row=7, column=0)
data_frame = tk.Frame(window)
data_frame.grid(row=8, column=0, columnspan=2)
window.mainloop()
```
这个界面包含了一些输入框和按钮,可以输入数据库连接信息和表名,然后连接数据库并显示表中的数据。请注意,这只是一个简单的示例,可能需要根据实际情况进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)