此处使用python自带最易上手的tkinter模块
参考网址:https://www.cnblogs.com/xiehy/p/10825970.html
功能为整合python日常自动化办公
main.py
from LoginPage import *
#初始化Tk()
root = Tk()
#设置标题
root.title('冯宝宝的工具箱')
#调度登录功能类
LoginPage(root)
#进入消息循环
root.mainloop()
LoginPage.py
from tkinter import *
from tkinter.messagebox import *
from MainPage import *
class LoginPage(object):
#对象初始化
def __init__(self, master=None):
self.root = master # 定义内部变量root
# 设置窗口大小
width = 310
height = 200
# 获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
screenwidth = self.root.winfo_screenwidth()
screenheight = self.root.winfo_screenheight()
# 设置窗口大小布局
self.root.geometry('%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2))
# 设置窗口是否可变长、宽,True:可变,False:不可变
self.root.resizable(width=False, height=False)
#设置登录用户名\密码
self.username = StringVar()
self.password = StringVar()
#调度自定义函数,部署窗口框架
self.createPage()
#部署窗口框架
def createPage(self):
# 创建Frame容器 框架控件
self.page = Frame(self.root)
#容器位置设置:pack方式,
self.page.pack() #side='left'设置主窗口位置top bottom left right
Label(self.page).grid(row=0, stick=W) #Label标签占一行 sticky=W 设定该值可以保证本行保持左对齐,N:北/上对齐 S:南/下对齐 W:西/左对齐 E:东/右对齐
Label(self.page, text='账号: ').grid(row=1, stick=W, pady=10) #padx=水平边距, pady=垂直边距
Entry(self.page, textvariable=self.username).grid(row=1, column=1, stick=E) #输入控件接收文本
Label(self.page, text='密码: ').grid(row=2, stick=W, pady=10)
Entry(self.page, textvariable=self.password, show='*').grid(row=2, column=1, stick=E)
Button(self.page, text='登陆', command=self.loginCheck).grid(row=3, stick=W, pady=10)
Button(self.page, text='退出', command=self.page.quit).grid(row=3, column=1, stick=E)
#登录验证
def loginCheck(self):
name = self.username.get()
secret = self.password.get()
if name == '1' and secret == '1':
self.page.destroy() #当前页面销毁
MainPage(self.root) #跳转功能主页面
else:
showinfo(title='错误', message='账号或密码错误!')
MainPage.py
from tkinter import *
from view import * # 菜单栏对应的各个子页面
class MainPage(object):
#对象初始化
def __init__(self, master=None):
self.root = master # 定义内部变量root
# 设置窗口大小
width = 600
height = 400
# 获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
screenwidth = self.root.winfo_screenwidth()
screenheight = self.root.winfo_screenheight()
# 设置窗口大小布局
self.root.geometry('%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2))
# 设置窗口是否可变长、宽,True:可变,False:不可变
self.root.resizable(width=False, height=False)
#调度自定义函数,部署窗口框架
self.createPage()
def createPage(self):
self.gnbPage = GnbFrame(self.root) # 功能包界面Frame
self.gn1Page = Gn1Frame(self.root)
self.gn2Page = Gn2Frame(self.root)
self.gn3Page = Gn3Frame(self.root)
self.gnbPage.pack() # 默认显示功能包界面
menubar = Menu(self.root) #菜单栏
menubar.add_command(label='功能包', command=self.gnbData)
menubar.add_command(label='自毁电脑', command=self.gn1Data)
menubar.add_command(label='超级外挂', command=self.gn2Data)
menubar.add_command(label='座椅飞天', command=self.gn3Data)
self.root['menu'] = menubar # 设置菜单栏
def gnbData(self):
self.gnbPage.pack()
self.gn1Page.pack_forget()
self.gn2Page.pack_forget()
self.gn3Page.pack_forget()
def gn1Data(self):
self.gnbPage.pack_forget()
self.gn1Page.pack()
self.gn2Page.pack_forget()
self.gn3Page.pack_forget()
def gn2Data(self):
self.gnbPage.pack_forget()
self.gn1Page.pack_forget()
self.gn2Page.pack()
self.gn3Page.pack_forget()
def gn3Data(self):
self.gnbPage.pack_forget()
self.gn1Page.pack_forget()
self.gn2Page.pack_forget()
self.gn3Page.pack()
view.py
from tkinter import *
from Modes.Md5Anal import *
from Modes.ExcelHb import *
class GnbFrame(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master # 定义内部变量root
self.createPage()
def createPage(self):
Label(self).grid(row=0, stick=W, pady=10)
Label(self, text='1.表格内容合并 ').grid(row=1, stick=W, pady=10)
Button(self, text='进入', bg="lightblue", command=excelHb_start).grid(row=1, column=1, stick=E, pady=10) #command=excelHb_start 注意这里的excelHb_start方法不能带括号excelHb_start() 否则会自动触发
Label(self, text='2.图片切两半 ').grid(row=2, stick=W, pady=10)
Button(self, text='进入', bg="lightblue").grid(row=2, column=1, stick=E, pady=10)
Label(self, text='3. ').grid(row=3, stick=W, pady=10)
Button(self, text='进入', bg="lightblue").grid(row=3, column=1, stick=W, pady=10)
Label(self, text='4.文本MD5加密').grid(row=4, stick=W, pady=10)
Button(self, text='进入',bg="lightblue", command=md5anal_start).grid(row=4, column=1, stick=W, pady=10)
class Gn1Frame(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master # 定义内部变量root
self.itemName = StringVar()
self.createPage()
def createPage(self):
Frame(bg='blue')
Label(self, text='宝宝爱你呦~',font=('KAITI 12 bold'),width=20,height=5).grid(stick=W, pady=100) #padx=水平边距, pady=垂直边距
class Gn2Frame(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master # 定义内部变量root
self.createPage()
def createPage(self):
Label(self, text='biubiubiubiu~',font=('KAITI 12 bold'),width=20,height=5).grid(stick=W, pady=100) #padx=水平边距, pady=垂直边距
class Gn3Frame(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master # 定义内部变量root
self.createPage()
def createPage(self):
Label(self, text=' ~~~ ~~~~\n ~~~~~~~ ~~~~~~~~\n ~~~~~~~~~~ ~~~~~~~~~~~\n ~~~~~~冯冯小甜心~~~~~\n ~~~~ ~~~~\n ~~~~~~~~~~~~~~\n ~~~~~~~~~\n ~~~\n',
font=('KAITI 12 bold'),
width=40,
height=10).grid(stick=W, pady=100) #padx=水平边距, pady=垂直边距
Md5Anal.py
from tkinter import *
import hashlib
import time
#文本MD5加密功能包
LOG_LINE_NUM = 0
class Md5Anal():
def __init__(self,init_window_name):
self.root = init_window_name
#设置窗口
def set_init_window(self):
self.root.title("文本MD5加密") #窗口名
self.root.geometry('1068x711+400+200') #290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
self.root.resizable(width=False, height=False) # 设置窗口是否可变长、宽,True:可变,False:不可变
self.root["bg"] = "WhiteSmoke" #窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
#标签
self.zw_label = Label(self.root,width=4) #占位标签
self.zw_label.grid()
self.init_data_label = Label(self.root, text="文本输入")
self.init_data_label.grid(row=1, column=1)
self.result_data_label = Label(self.root, text="输出结果")
self.result_data_label.grid(row=1, column=13)
self.log_label = Label(self.root, text="日志")
self.log_label.grid(row=13, column=1)
#文本框
self.init_data_Text = Text(self.root, width=60, height=35) #原始数据录入框
self.init_data_Text.grid(row=2, column=1, rowspan=10, columnspan=10)
self.result_data_Text = Text(self.root, width=70, height=49) #处理结果展示
self.result_data_Text.grid(row=2, column=13, rowspan=15, columnspan=10)
self.log_data_Text = Text(self.root, width=60, height=12) # 日志框
self.log_data_Text.grid(row=14, column=1, columnspan=10)
#按钮
self.str_trans_to_md5_button = Button(self.root, text="加密", bg="lightblue", width=10,command=self.str_trans_to_md5) # 调用内部方法 加()为直接调用
self.str_trans_to_md5_button.grid(row=6, column=12)
#功能函数
def str_trans_to_md5(self):
src = self.init_data_Text.get(1.0,END).strip().replace("\n","").encode()
if src:
try:
myMd5 = hashlib.md5()
myMd5.update(src)
myMd5_Digest = myMd5.hexdigest()
#输出到界面
self.result_data_Text.delete(1.0,END)
self.result_data_Text.insert(1.0,myMd5_Digest)
self.write_log_to_Text("加密成功!!")
except Exception as e:
self.result_data_Text.delete(1.0,END)
self.result_data_Text.insert(1.0,"加密失败,报错代码:"+e)
else:
self.write_log_to_Text("请输入~")
#日志动态打印
def write_log_to_Text(self,logmsg):
global LOG_LINE_NUM
current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
logmsg_in = str(current_time) +":" + str(logmsg) + "\n" #换行
if LOG_LINE_NUM <= 11:
self.log_data_Text.insert(END, logmsg_in)
LOG_LINE_NUM = LOG_LINE_NUM + 1
else:
self.log_data_Text.delete(1.0,2.0)
self.log_data_Text.insert(END, logmsg_in)
def md5anal_start():
init_window = Tk() #实例化出一个父窗口
ZMJ_PORTAL = Md5Anal(init_window)
# 设置根窗口默认属性
ZMJ_PORTAL.set_init_window()
init_window.mainloop() #父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示
ExcelHb.py
from tkinter import *
import time
import os
import xlrd
import pandas as pd
#Excel内容合并功能包
LOG_LINE_NUM = 0
class ExcelHb():
def __init__(self,init_window_name):
self.root = init_window_name
#设置窗口
def set_init_window(self):
self.root.title("表格内容合并") #窗口名
self.root.geometry('1068x711+400+200') #290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
self.root.resizable(width=False, height=False) # 设置窗口是否可变长、宽,True:可变,False:不可变
self.root["bg"] = "WhiteSmoke" #窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
#标签
self.zw_label = Label(self.root,width=4) #占位标签
self.zw_label.grid()
self.init_data_label = Label(self.root, text="文本输入")
self.init_data_label.grid(row=1, column=1)
self.result_data_label = Label(self.root, text="输出结果")
self.result_data_label.grid(row=1, column=13)
self.log_label = Label(self.root, text="日志")
self.log_label.grid(row=13, column=1)
#文本框
self.init_data_Text = Text(self.root, width=60, height=35) #原始数据录入框
self.init_data_Text.grid(row=2, column=1, rowspan=10, columnspan=10)
self.result_data_Text = Text(self.root, width=70, height=49) #处理结果展示
self.result_data_Text.grid(row=2, column=13, rowspan=15, columnspan=10)
self.log_data_Text = Text(self.root, width=60, height=12) # 日志框
self.log_data_Text.grid(row=14, column=1, columnspan=10)
#按钮-功能调度
self.func_main_button = Button(self.root, text="执行", bg="lightblue", width=10,command=self.func_main) # 调用内部方法 加()为直接调用
self.func_main_button.grid(row=6, column=12)
#功能函数
def func_main(self):
src = self.init_data_Text.get(1.0,END).strip().replace("\n","").encode().decode()
if src:
try:
file_ph_res = os.path.dirname(src) + '\\合并后' # 取文件路径的上级路径,生成文件夹
wfile = file_ph_res + '\\合并后.csv'
# 若合并后文件路径不存在,创建
if not os.path.isdir(file_ph_res):
os.mkdir(file_ph_res)
is_ok = 0 # 解析文件计数器
r_excel = []
for root, dirs, files in os.walk(src):
if files:
for file in files:
file_name = root + '\\' + file
if '.xls' in file_name or '.xlsx' in file_name:
self.write_log_to_Text("处理中:"+file)
for sheet_i in range(xlrd.open_workbook(file_name).nsheets):
re = xlrd.open_workbook(file_name).sheet_by_index(sheet_i)
if re.visibility == 0: # 只合并未隐藏的页签;0 可见 1=隐藏(可通过“用户格式”—“工作表”—“取消隐藏”取消隐藏) 2=“非常隐藏”(只能通过VBA宏取消隐藏)
if re.nrows > 0 and re.cell(0, 0).value:
r_excel.append(pd.read_excel(file_name, sheet_i))
elif re.nrows > 0 and len(re.cell(0, 0).value) == 0:
r_excel.append(pd.read_excel(file_name, sheet_i, 1))
is_ok += 1
df = pd.DataFrame(pd.concat(r_excel)).dropna(axis=1, how='all') # axis=1指列 how=‘any’只要列中含有一个空值
df.to_csv(wfile,
header=True, # 是否保存列索引
index=False, # 是否保存⾏索引,保存⾏索引,⽂件被加载时,默认⾏索引会作为⼀列
encoding="GBK")
if is_ok != 0:
self.result_data_Text.delete(1.0, END)
self.result_data_Text.insert(1.0, "合并后文件:"+wfile)
self.write_log_to_Text("解析完成,合并成功!")
else:
self.write_log_to_Text("解析失败,未检测到Excel!")
except Exception as e:
self.result_data_Text.delete(1.0,END)
self.result_data_Text.insert(1.0,"解析失败,报错代码:"+str(e))
else:
self.result_data_Text.delete(1.0, END)
self.write_log_to_Text("请输入~")
#日志动态打印
def write_log_to_Text(self,logmsg):
global LOG_LINE_NUM
current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
logmsg_in = str(current_time) +":" + str(logmsg) + "\n" #换行
if LOG_LINE_NUM <= 11:
self.log_data_Text.insert(END, logmsg_in)
LOG_LINE_NUM = LOG_LINE_NUM + 1
else:
self.log_data_Text.delete(1.0,2.0)
self.log_data_Text.insert(END, logmsg_in)
def excelHb_start():
init_window = Tk() #实例化出一个父窗口
ZMJ_PORTAL = ExcelHb(init_window)
# 设置根窗口默认属性
ZMJ_PORTAL.set_init_window()
init_window.mainloop() #父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示