Python Tkinter 综合查询窗口 - 查询北京朝阳区民警信息
import tkinter as tk
from tkinter import ttk
import pymysql as py
def zonghechaxunwindow():
def select1():
tree = ttk.treeview(zonghechaxunwindow)
tree['columns'] = ('id', 'name', 'gender', 'phone', 'address', 'remark')
tree.column('id', width=90)
tree.column('name', width=90)
tree.column('gender', width=90)
tree.column('phone', width=90)
tree.column('address', width=90)
tree.column('remark', width=90)
tree.heading('id', text='身份证号')
tree.heading('name', text='姓名')
tree.heading('gender', text='性别')
tree.heading('phone', text='联系电话')
tree.heading('address', text='家庭住址')
tree.heading('remark', text='职位')
tree.grid(column=0, row=1, sticky='NSEW')
conn = py.connect(host='localhost', user='root', password='houyushan1022', db='police')
cursor = conn.cursor()
cursor.execute('select * from person_info where 家庭住址='北京市朝阳区'')
list_result = cursor.fetchall()
x = tree.get_children()
for item in x:
tree.delete(item)
for i in range(len(list_result)):
tree.insert('', i, text=str(i), values=(list_result[i][0], list_result[i][1], list_result[i][2], list_result[i][3], list_result[i][4], list_result[i][5]))
conn.commit()
conn.close()
select_button1 = tk.Button(zonghechaxunwindow, bg='pink', text='查询家在北京朝阳区的民警', font=('微软雅黑 -20'), width=40, height=1, command=select1)
select_button1.place(x=10, y=370)
window = tk.Toplevel() # 修改此行
window.geometry('800x500')
window.title('综合查询')
window.iconbitmap('favicon.ico')
window.resizable(0, 0)
# 运行窗口
zonghechaxunwindow()
代码修改:
- 将
ttk.Toplevel()改为tk.Toplevel(),因为Toplevel是tk中的一个类,而不是ttk中的。 - 添加了一个
window.iconbitmap('favicon.ico')行,用于设置窗口的图标。请将favicon.ico替换为你的图标文件路径。
代码功能:
- 使用 Python
Tkinter库创建综合查询窗口。 - 使用
ttk.treeview创建一个树形视图控件,用于显示查询结果。 - 使用
tk.Button创建一个按钮,用于触发查询操作。 - 通过连接数据库,查询家在北京朝阳区的民警信息,并将结果显示在树形视图控件中。
- 可以根据需要修改数据库连接信息和查询语句。
代码使用:
- 确保你已经安装了
tkinter和pymysql库。 - 将代码保存为
.py文件。 - 运行该文件。
注意:
- 请确保数据库连接信息正确。
- 可以根据需要修改窗口大小、按钮文本和查询语句等。
- 可以添加更多功能,例如搜索、排序等。
原文地址: https://www.cveoy.top/t/topic/osp2 著作权归作者所有。请勿转载和采集!