Skip to content

Commit

Permalink
增加了随机密码生成器
Browse files Browse the repository at this point in the history
  • Loading branch information
cforth committed Feb 23, 2018
1 parent 809f93f commit 54003d8
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 16 deletions.
38 changes: 22 additions & 16 deletions CryptoApp.pyw
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ import tkinter.ttk as ttk
import tkinter.messagebox as messagebox
import ImgLook
import Cryptor
import RandomPassword


class TopWindow(ttk.Frame):
class RootWindow(ttk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.menubar = tk.Menu(self)
self.ImgLookWindow = None
self.CryptoWindow = None
self.ChildWindow = None
self.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.E, tk.W))
self.master.minsize(100, 40)
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)

option_menu = tk.Menu(self.menubar, tearoff=0)
option_menu.add_command(label="图片查看", command=self.img_look)
option_menu.add_command(label="随机密码", command=self.random_password)
option_menu.add_command(label="加密解密", command=self.crypto)
option_menu.add_separator()
option_menu.add_command(label="退出", command=self.master.quit)
Expand All @@ -33,30 +35,34 @@ class TopWindow(ttk.Frame):
self.master.config(menu=self.menubar)

def clear_window(self):
if self.ImgLookWindow:
self.ImgLookWindow.destroy()
self.ImgLookWindow = None
elif self.CryptoWindow:
self.CryptoWindow.destroy()
self.CryptoWindow = None
if self.ChildWindow:
self.ChildWindow.destroy()
self.ChildWindow = None

def img_look(self):
self.clear_window()
self.ImgLookWindow = ImgLook.Window("ImgLookUI.json", self)
self.ChildWindow = ImgLook.Window("ImgLookUI.json", self)
self.master.title("图片查看器")
self.master.minsize(600, 150)

def random_password(self):
self.clear_window()
self.ChildWindow = RandomPassword.Window("RandomPasswordUI.json", self)
self.master.title("随机密码生成器")
self.master.minsize(600, 30)

def crypto(self):
self.clear_window()
self.CryptoWindow = Cryptor.Window(self)
self.ChildWindow = Cryptor.Window(self)
self.master.title("加密解密器")
self.master.minsize(400, 650)

def help_about(self):
messagebox.showinfo('关于', 'CF工具箱V1.0') # 弹出消息提示框

def main():

if __name__ == '__main__':
master = tk.Tk()
mb = TopWindow(master)
mb = RootWindow(master)
mb.crypto()
master.mainloop()

main()
1 change: 1 addition & 0 deletions Cryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,6 @@ def converter(self):
app = Window()
# 设置窗口标题:
app.master.title("CF加密解密器")
app.master.minsize(400, 500)
# 主消息循环:
app.mainloop()
1 change: 1 addition & 0 deletions ImgLook.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,6 @@ def img_show(self, event=None):
app = Window("ImgLookUI.json")
# 设置窗口标题:
app.master.title("图片查看器")
app.master.minsize(600, 150)
# 主消息循环:
app.mainloop()
60 changes: 60 additions & 0 deletions RandomPassword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import random
from libs.json2gui import *


LOW_LEVEL = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']

MEDIUM_LEVEL = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0']

HIGH_LEVEL = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'!', '@', '#', '$', '%', '^', '&', '*', '-', '_', '=',
'+', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']


# 根据密码长度与复杂度,生成随机密码
def generate_password(length, level=LOW_LEVEL):
return ''.join([random.choice(level) for i in range(0, length)])


# 窗口类
class Window(ttk.Frame):
def __init__(self, ui_json, master=None):
super().__init__(master)
# 从json自动设置UI控件
create_ui(self, ui_json)
# 从json自动绑定事件
create_all_binds(self, ui_json)
set_combobox_item(self.__dict__["passLevelCombobox"], "中等", True)
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)
self.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.E, tk.W))
self.columnconfigure(4, weight=1)

def generate_password_button_callback(self, event=None):
length = int(self.__dict__["pass_length"].get())
level = MEDIUM_LEVEL
level_text = self.__dict__["pass_level"].get()

if level_text == "简单":
level = LOW_LEVEL
elif level_text == "复杂":
level = HIGH_LEVEL

password = generate_password(length, level)
self.__dict__["password"].set(password)


if __name__ == '__main__':
app = Window("RandomPasswordUI.json")
# 设置窗口标题:
app.master.title("随机密码生成器")
app.master.minsize(600, 30)
# 主消息循环:
app.mainloop()
40 changes: 40 additions & 0 deletions RandomPasswordUI.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"passLengthLabel": {
"class": "Label",
"int": {"width": 10},
"string": {"text": "密码长度: "},
"grid": {"row":0, "column":0}
},
"passLengthEntry": {
"class": "Entry",
"var": "pass_length",
"int": {"width": 10},
"grid": {"row":0, "column":1, "sticky":["W", "E"]}
},
"passLevelLabel": {
"class": "Label",
"int": {"width": 10},
"string": {"text": "密码强度: "},
"grid": {"row":0, "column":2}
},
"passLevelCombobox": {
"class": "Combobox",
"var": "pass_level",
"int": {"width": 10},
"string": {"state": ["readonly"], "values": ["简单", "中等", "复杂"]},
"grid": {"row":0, "column":3, "sticky":["W", "E"]}
},
"passwordEntry": {
"class": "Entry",
"var": "password",
"int": {"width": 40},
"grid": {"row":0, "column":4, "sticky":["W", "E"]}
},
"generatePasswordButton": {
"class": "Button",
"int": {"width": 5},
"string": {"text": "生成"},
"command": "generate_password_button_callback",
"grid": {"row":0, "column":5, "sticky":["W", "E"]}
}
}

0 comments on commit 54003d8

Please sign in to comment.