python实现GUI设计打开文件命令源码DIY另含字符串转MD5的python源码文本处理工具。
源码如下:#!/usr/bin/env python# -*- coding:utf-8 -*-# Author's_name_is_NIKOLA_SSimport osfrom time import sleepfrom tkinter import *class DirList(object):def __init__(self, initdir=None):self.top = Tk()se
·
tkinter布局设定的相关参数,属性配置。
源码如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author's_name_is_NIKOLA_SS
import os
from time import sleep
from tkinter import *
class DirList(object):
def __init__(self, initdir=None):
self.top = Tk()
self.label = Label(self.top,
text='Directory Lister made by NIKOLA_SS')
self.label.pack()
self.cwd=StringVar(self.top)
self.dirl = Label(self.top, fg='blue',
font=('Helvetica', 12, 'bold'))
self.dirl.pack()
self.dirfm = Frame(self.top)
self.dirsb = Scrollbar(self.dirfm)
self.dirsb.pack(side=RIGHT, fill=Y)
self.dirs = Listbox(self.dirfm, height=15,
width=50, yscrollcommand=self.dirsb.set)
self.dirs.bind('<Double-1>', self.setdirandgo)
self.dirsb.config(command=self.dirs.yview)
self.dirs.pack(side=LEFT, fill=BOTH)
self.dirfm.pack()
self.dirn = Entry(self.top, width=50,
textvariable=self.cwd)
self.dirn.bind('<Return>', self.dols)
self.dirn.pack()
self.bfm = Frame(self.top)
self.clr = Button(self.bfm, text='Clear',
command=self.clrdir,
activeforeground='white',
activebackground='blue')
self.ls = Button(self.bfm,
text='List Directory',
command=self.dols,
activeforeground='white',
activebackground='green')
self.quit = Button(self.bfm, text='Quit',
command=self.top.quit,
activeforeground='white',
activebackground='red')
self.clr.pack(side=LEFT)
self.ls.pack(side=LEFT)
self.quit.pack(side=LEFT)
self.bfm.pack()
if initdir:
self.cwd.set(os.curdir)
self.dols()
def clrdir(self, ev=None):
self.cwd.set('')
def setdirandgo(self, ev=None):
self.last = self.cwd.get()
self.dirs.config(selectbackground='red')
check = self.dirs.get(self.dirs.curselection())
if not check:
check = os.curdir
self.cwd.set(check)
self.dols()
def dols(self, ev=None):
error = ''
tdir = self.cwd.get()
if not tdir:
tdir = os.curdir
if not os.path.exists(tdir):
error = tdir + ': no such file'
elif not os.path.isdir(tdir):
error = tdir + ': not a directory'
if error:
self.cwd.set(error)
self.top.update()
sleep(2)
if not (hasattr(self, 'last') \
and self.last):
self.last = os.curdir
self.cwd.set(self.last)
self.dirs.config(
selectbackground='LightSkyBlue')
self.top.update()
return
self.cwd.set(
'FETCHING DIRECTORY CONTENTS...')
self.top.update()
dirlist = os.listdir(tdir)
dirlist.sort()
os.chdir(tdir)
self.dirl.config(text=os.getcwd())
self.dirs.delete(0, END)
self.dirs.insert(END, os.curdir)
self.dirs.insert(END, os.pardir)
for eachFile in dirlist:
self.dirs.insert(END, eachFile)
self.cwd.set(os.curdir)
self.dirs.config(
selectbackground='LightSkyBlue')
def main():
d = DirList(os.curdir)
mainloop()
if __name__ == '__main__':
main()
随便玩玩。。问题自己调试吧。
其他布局配置相关的位置要求。
row:指定行
column:指定列
padx:控件离水平左右间距
pady:控件离垂直上下间距
sticky:N上对齐S下对齐E左对齐W右对齐
columnspan:合并一行中指定个列
rowspan:合并一列中指定个行
参考来源:https://www.runoob.com/python/python-gui-tkinter.html等等。
字符串转MD5工具GUI设计。
源码如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tkinter import *
import hashlib
import time
LOG_LINE_NUM = 0
class MY_GUI():
def __init__(self,init_window_name):
self.init_window_name = init_window_name
#设置窗口
def set_init_window(self):
self.init_window_name.title("文本处理工具Ver21") #窗口名
#self.init_window_name.geometry('320x160+10+10') #290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
self.init_window_name.geometry('1068x681+10+10')
#self.init_window_name["bg"] = "pink" #窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
#self.init_window_name.attributes("-alpha",0.9) #虚化,值越小虚化程度越高
#标签
self.init_data_label = Label(self.init_window_name, text="待处理数据")
self.init_data_label.grid(row=0, column=0)
self.result_data_label = Label(self.init_window_name, text="输出结果")
self.result_data_label.grid(row=0, column=12)
self.log_label = Label(self.init_window_name, text="日志")
self.log_label.grid(row=12, column=0)
#文本框
self.init_data_Text = Text(self.init_window_name, width=67, height=35) #原始数据录入框
self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
self.result_data_Text = Text(self.init_window_name, width=70, height=49) #处理结果展示
self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
self.log_data_Text = Text(self.init_window_name, width=66, height=9) # 日志框
self.log_data_Text.grid(row=13, column=0, columnspan=10)
#按钮
self.str_trans_to_md5_button = Button(self.init_window_name, text="字符串转MD5", bg="lightblue", width=10,command=self.str_trans_to_md5) # 调用内部方法 加()为直接调用
self.str_trans_to_md5_button.grid(row=1, column=11)
#功能函数
def str_trans_to_md5(self):
src = self.init_data_Text.get(1.0,END).strip().replace("\n","").encode()
#print("src =",src)
if src:
try:
myMd5 = hashlib.md5()
myMd5.update(src)
myMd5_Digest = myMd5.hexdigest()
#print(myMd5_Digest)
#输出到界面
self.result_data_Text.delete(1.0,END)
self.result_data_Text.insert(1.0,myMd5_Digest)
self.write_log_to_Text("INFO:str_trans_to_md5 success")
except:
self.result_data_Text.delete(1.0,END)
self.result_data_Text.insert(1.0,"字符串转MD5失败")
else:
self.write_log_to_Text("ERROR:str_trans_to_md5 failed")
#获取当前时间
def get_current_time(self):
current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
return current_time
#日志动态打印
def write_log_to_Text(self,logmsg):
global LOG_LINE_NUM
current_time = self.get_current_time()
logmsg_in = str(current_time) +" " + str(logmsg) + "\n" #换行
if LOG_LINE_NUM <= 7:
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 gui_start():
init_window = Tk() #实例化出一个父窗口
ZMJ_PORTAL = MY_GUI(init_window)
# 设置根窗口默认属性
ZMJ_PORTAL.set_init_window()
init_window.mainloop()
#父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示
gui_start()
更多推荐
已为社区贡献8条内容
所有评论(0)