帮我写一个用python tkinter 写的记录人员信息并且把信息存到MySQL数据库记录人员的姓名,地址,金额,备注
时间: 2023-03-29 17:01:28 浏览: 67
好的,我可以回答这个问题。以下是一个简单的 Python tkinter 程序,用于记录人员信息并将其存储到 MySQL 数据库中:
```python
import tkinter as tk
import mysql.connector
# 创建 MySQL 数据库连接
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
# 创建 MySQL 数据库表
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE IF NOT EXISTS people (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255), amount FLOAT, note VARCHAR(255))")
# 创建 tkinter 窗口
window = tk.Tk()
window.title("记录人员信息")
# 创建标签和输入框
tk.Label(window, text="姓名").grid(row=, column=)
name_entry = tk.Entry(window)
name_entry.grid(row=, column=1)
tk.Label(window, text="地址").grid(row=1, column=)
address_entry = tk.Entry(window)
address_entry.grid(row=1, column=1)
tk.Label(window, text="金额").grid(row=2, column=)
amount_entry = tk.Entry(window)
amount_entry.grid(row=2, column=1)
tk.Label(window, text="备注").grid(row=3, column=)
note_entry = tk.Entry(window)
note_entry.grid(row=3, column=1)
# 创建保存按钮
def save():
name = name_entry.get()
address = address_entry.get()
amount = float(amount_entry.get())
note = note_entry.get()
# 将数据插入到 MySQL 数据库表中
sql = "INSERT INTO people (name, address, amount, note) VALUES (%s, %s, %s, %s)"
val = (name, address, amount, note)
mycursor.execute(sql, val)
mydb.commit()
# 清空输入框
name_entry.delete(, tk.END)
address_entry.delete(, tk.END)
amount_entry.delete(, tk.END)
note_entry.delete(, tk.END)
tk.Button(window, text="保存", command=save).grid(row=4, column=1)
window.mainloop()
```
这个程序创建了一个 tkinter 窗口,其中包含四个标签和输入框,用于输入人员的姓名、地址、金额和备注。当用户点击保存按钮时,程序将获取输入框中的数据,并将其插入到 MySQL 数据库表中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)