用Python tkinter帮我写一个提交数据的窗体,窗体的大小为300x300 提交的数据分别是 姓名,金额,备注,时间,把所有数据相应的控件用place把控件位置居中,并且用pymysql把数据提交到数据库,
时间: 2023-09-11 08:04:38 浏览: 145
对Python 窗体(tkinter)树状数据(Treeview)详解
### 回答1:
好的,我可以回答这个问题。以下是Python tkinter的代码示例,可以帮您创建一个提交数据的窗体,并将数据提交到数据库:
```python
import tkinter as tk
import pymysql
# 创建窗口
window = tk.Tk()
window.title("提交数据")
window.geometry("300x300")
# 创建标签和输入框
name_label = tk.Label(window, text="姓名:")
name_label.place(relx=.5, rely=.2, anchor="center")
name_entry = tk.Entry(window)
name_entry.place(relx=.5, rely=.3, anchor="center")
amount_label = tk.Label(window, text="金额:")
amount_label.place(relx=.5, rely=.4, anchor="center")
amount_entry = tk.Entry(window)
amount_entry.place(relx=.5, rely=.5, anchor="center")
note_label = tk.Label(window, text="备注:")
note_label.place(relx=.5, rely=.6, anchor="center")
note_entry = tk.Entry(window)
note_entry.place(relx=.5, rely=.7, anchor="center")
time_label = tk.Label(window, text="时间:")
time_label.place(relx=.5, rely=.8, anchor="center")
time_entry = tk.Entry(window)
time_entry.place(relx=.5, rely=.9, anchor="center")
# 创建提交按钮
def submit_data():
name = name_entry.get()
amount = amount_entry.get()
note = note_entry.get()
time = time_entry.get()
# 连接数据库
db = pymysql.connect(host="localhost", user="root", password="password", database="test")
cursor = db.cursor()
# 插入数据
sql = "INSERT INTO data (name, amount, note, time) VALUES (%s, %s, %s, %s)"
val = (name, amount, note, time)
cursor.execute(sql, val)
db.commit()
# 关闭数据库连接
db.close()
submit_button = tk.Button(window, text="提交", command=submit_data)
submit_button.place(relx=.5, rely=.95, anchor="center")
# 运行窗口
window.mainloop()
```
这个窗体包含四个输入框和一个提交按钮。当用户点击提交按钮时,程序将获取输入框中的数据,并将其插入到名为"data"的数据库表中。您需要将代码中的数据库连接信息和表名替换为您自己的信息。
### 回答2:
import tkinter as tk
import pymysql
def submit_data():
name = name_entry.get()
amount = amount_entry.get()
note = note_entry.get()
time = time_entry.get()
connection = pymysql.connect(host='localhost', user='root', password='password', database='mydatabase')
cursor = connection.cursor()
query = "INSERT INTO mytable (name, amount, note, time) VALUES (%s, %s, %s, %s)"
cursor.execute(query, (name, amount, note, time))
connection.commit()
connection.close()
window = tk.Tk()
window.geometry("300x300")
name_label = tk.Label(window, text="姓名")
name_label.place(x=50, y=50, anchor="center")
name_entry = tk.Entry(window)
name_entry.place(x=150, y=50, anchor="center")
amount_label = tk.Label(window, text="金额")
amount_label.place(x=50, y=100, anchor="center")
amount_entry = tk.Entry(window)
amount_entry.place(x=150, y=100, anchor="center")
note_label = tk.Label(window, text="备注")
note_label.place(x=50, y=150, anchor="center")
note_entry = tk.Entry(window)
note_entry.place(x=150, y=150, anchor="center")
time_label = tk.Label(window, text="时间")
time_label.place(x=50, y=200, anchor="center")
time_entry = tk.Entry(window)
time_entry.place(x=150, y=200, anchor="center")
submit_button = tk.Button(window, text="提交", command=submit_data)
submit_button.place(x=150, y=250, anchor="center")
window.mainloop()
### 回答3:
使用Python的tkinter库可以很方便地创建一个数据提交的窗体。下面是一个示例代码,可以根据需要进行修改和优化。
```python
import tkinter as tk
import pymysql
def submit_data():
name = name_entry.get()
amount = amount_entry.get()
remark = remark_entry.get()
time = time_entry.get()
# 连接数据库
connection = pymysql.connect(host='localhost', user='username', password='password', db='database')
# 创建游标对象
cursor = connection.cursor()
# 执行插入数据的SQL语句
sql = "INSERT INTO table (name, amount, remark, time) VALUES (%s, %s, %s, %s)"
cursor.execute(sql, (name, amount, remark, time))
# 提交事务
connection.commit()
# 关闭游标和连接
cursor.close()
connection.close()
# 创建窗体
window = tk.Tk()
window.geometry('300x300')
# 创建姓名输入框和标签,并居中显示
name_label = tk.Label(window, text="姓名")
name_label.place(relx=0.5, rely=0.2, anchor='center')
name_entry = tk.Entry(window)
name_entry.place(relx=0.5, rely=0.3, anchor='center')
# 创建金额输入框和标签,并居中显示
amount_label = tk.Label(window, text="金额")
amount_label.place(relx=0.5, rely=0.4, anchor='center')
amount_entry = tk.Entry(window)
amount_entry.place(relx=0.5, rely=0.5, anchor='center')
# 创建备注输入框和标签,并居中显示
remark_label = tk.Label(window, text="备注")
remark_label.place(relx=0.5, rely=0.6, anchor='center')
remark_entry = tk.Entry(window)
remark_entry.place(relx=0.5, rely=0.7, anchor='center')
# 创建时间输入框和标签,并居中显示
time_label = tk.Label(window, text="时间")
time_label.place(relx=0.5, rely=0.8, anchor='center')
time_entry = tk.Entry(window)
time_entry.place(relx=0.5, rely=0.9, anchor='center')
# 创建提交按钮并居中显示
submit_button = tk.Button(window, text="提交", command=submit_data)
submit_button.place(relx=0.5, rely=0.95, anchor='center')
# 运行窗体主循环
window.mainloop()
```
请注意,根据实际情况修改数据库的连接信息(host、user、password、db)以及要插入数据的表名(table)。这段代码只是一个示例,具体的数据库操作逻辑可能需要根据实际需求做进一步优化。
阅读全文