#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# pip3 install PyMySQL
import tkinter as tk
import pymysql
import csv
import os
window = tk.Tk()
# 标题
window.title('天眼查小工具')
# 窗口大小
window.geometry('500x500')
# 请输入关键字提示文字
label1 = tk.Label(window, text="请输入关键字:")
# 下方提示文字
label2 = tk.Label(window, text="输入关键字点击查询按钮即可搜索,点击导出数据即可导出为excel文件。")
# 关键字输入框
company_name_input = tk.Entry(window, show=None, font=('微软雅黑', 14))
# 查询结果显示文本框
text = tk.Text(window, width=39, height=14, font=('微软雅黑', 15))
# 连接数据库
db = pymysql.connect("localhost", "root", "", "holder")
cursor = db.cursor()
success_con = "程序启动成功。\n数据库连接成功。\n输入关键字开始查询吧..."
text.insert("insert", success_con)
export_res = ()
# 清空
def reset():
company_name_input.delete(0, "end")
text.delete(1.0, "end")
# 查询
def search():
text.delete(1.0, "end")
key_word = company_name_input.get()
key_word = str(key_word).lstrip().rstrip()
do_search(key_word)
# 导出
def export():
if len(export_res) == 0:
text.delete(1.0, "end")
text.insert("insert", '内容为空,无法导出!')
file_path = 'data.csv'
if os.path.exists(file_path):
os.remove(file_path)
csv_file = open(file_path, 'a+', newline='')
try:
writer = csv.writer(csv_file)
header = ('股东', '公司', 'Account UserName', 'Primary UserName', 'Account Segment', 'AM Name')
writer.writerow(header)
for res in export_res:
writer.writerow(res)
path = os.path.abspath(file_path)
text.insert("insert", '---------------\n文件导出完成!\n文件地址:' + path)
finally:
csv_file.close()
# 查询按钮
button_search = tk.Button(window, text="查询", bg="LightBlue", font=('微软雅黑', 12), width=6, height=1, command=search)
# 清空按钮
button_reset = tk.Button(window, text="清空", bg="LightCyan", font=('微软雅黑', 12), width=6, height=1, command=reset)
# 导出按钮
button_export = tk.Button(window, text="导出数据", font=('微软雅黑', 12), width=8, height=1, command=export)
def do_search(key_word):
content = "未查询到任何与" + key_word + "相关的结果!"
if len(key_word) != 0:
cursor.execute(
'select share,name,account_username,primary_username,account_segment,am_name from holder where share in (select share from holder where name like \'%' + key_word + '%\')')
result = cursor.fetchall()
global export_res
export_res = result
if len(result) != 0:
content = "共查询到" + str(len(result)) + "条与" + key_word + "相关的信息:\n"
for res in result:
content += res[0] + " - " + res[1] + " - " + res[2] + " - " + res[3] + " - " + res[
4] + " - " + res[5] + "\n"
else:
content = "请输入关键字!"
text.insert("insert", content)
db.commit()
def main():
# 请输入关键字提示文字坐标
label1.place(x=10, y=15)
# 关键字输入框坐标
company_name_input.place(x=100, y=15)
# 查询按钮坐标
button_search.place(x=340, y=12)
# 清空按钮坐标
button_reset.place(x=420, y=12)
# 查询结果显示文本框坐标
text.place(x=12, y=60)
# 导出按钮坐标
button_export.place(x=400, y=455)
# 下方提示文字坐标
label2.place(x=10, y=465)
window.mainloop()
if __name__ == '__main__':
main()

本文地址:https://blog.csdn.net/King__Cheung/article/details/110917604