帮我用python tkinter写一个记录人员信息的姓名,金额,地址,备注然后用pymysql模块将信息存到MySQL数据库中
时间: 2023-03-29 14:01:32 浏览: 55
好的,我可以回答这个问题。以下是一个简单的 Python Tkinter 界面,可以用来记录人员信息的姓名、金额、地址和备注。同时,使用 PyMySQL 模块将信息存储到 MySQL 数据库中。
```python
import tkinter as tk
import pymysql
# 创建 MySQL 数据库连接
conn = pymysql.connect(host='localhost', user='root', password='password', db='testdb')
# 创建游标对象
cursor = conn.cursor()
# 创建 Tkinter 窗口
root = tk.Tk()
root.title('人员信息记录')
# 创建标签和输入框
tk.Label(root, text='姓名').grid(row=, column=)
name_entry = tk.Entry(root)
name_entry.grid(row=, column=1)
tk.Label(root, text='金额').grid(row=1, column=)
amount_entry = tk.Entry(root)
amount_entry.grid(row=1, column=1)
tk.Label(root, text='地址').grid(row=2, column=)
address_entry = tk.Entry(root)
address_entry.grid(row=2, column=1)
tk.Label(root, text='备注').grid(row=3, column=)
comment_entry = tk.Entry(root)
comment_entry.grid(row=3, column=1)
# 定义保存信息的函数
def save_info():
name = name_entry.get()
amount = amount_entry.get()
address = address_entry.get()
comment = comment_entry.get()
# 将信息插入到 MySQL 数据库中
sql = "INSERT INTO person_info (name, amount, address, comment) VALUES (%s, %s, %s, %s)"
cursor.execute(sql, (name, amount, address, comment))
conn.commit()
# 清空输入框
name_entry.delete(, tk.END)
amount_entry.delete(, tk.END)
address_entry.delete(, tk.END)
comment_entry.delete(, tk.END)
# 创建保存按钮
save_button = tk.Button(root, text='保存', command=save_info)
save_button.grid(row=4, column=1)
# 运行 Tkinter 窗口
root.mainloop()
# 关闭数据库连接
cursor.close()
conn.close()
```
请注意,这只是一个简单的示例,您可能需要根据自己的需求进行修改和扩展。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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_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)