python tkinter窗口显示MySQL数据调到中间
时间: 2023-11-18 15:06:16 浏览: 105
你可以使用`place()`方法将窗口放置在屏幕的中间位置。下面是一个示例代码:
```python
import tkinter as tk
import pymysql
# 连接MySQL数据库
conn = pymysql.connect(
host='localhost',
user='root',
password='password',
db='test',
charset='utf8mb4'
)
cursor = conn.cursor()
# 查询数据
cursor.execute('SELECT * FROM students')
data = cursor.fetchall()
# 创建窗口
root = tk.Tk()
# 设置窗口大小和标题
root.geometry('400x300')
root.title('MySQL 数据')
# 创建标签
header = ['ID', 'Name', 'Age']
for i, col in enumerate(header):
tk.Label(root, text=col, font=('Arial', 14)).grid(row=0, column=i)
# 显示数据
for i, row in enumerate(data):
for j, col in enumerate(row):
tk.Label(root, text=col, font=('Arial', 12)).grid(row=i+1, column=j)
# 将窗口放置在屏幕中间
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width - root.winfo_reqwidth()) / 2
y = (screen_height - root.winfo_reqheight()) / 2
root.geometry("+%d+%d" % (x, y))
root.mainloop()
# 关闭数据库连接
cursor.close()
conn.close()
```
在这个示例代码中,我们使用`place()`方法将窗口放置在屏幕中间,然后使用`grid()`方法将标签和数据显示在窗口上。
阅读全文
相关推荐


















