# Create by ruiyang in 2022/12/16 ~ Version: Python 3.9
from tkinter import *
from pyperclip import *
from tkinter import messagebox
import logging
import re
class Home:
def __init__(self):
# 父容器
self.root = Tk()
self.root.geometry("400x140")
self.root.title("Tools")
# 设置图标,此处的False表示此图标图像仅适用于此特定窗口,但不适用于未来创建的顶层。
# self.root.iconphoto(False, PhotoImage(file='Pinkpig.PNG'))
# 可变变量
self.user_input = StringVar()
self.user_input_part = None
logging.basicConfig(level=logging.INFO, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.info('__init__ - succeed in setting variables')
def home(self):
self.destroy_all()
self.menu()
Label(self.root, text='\n\n欢迎使用Tools,左上角选择功能').pack()
logging.info('main_window - succeed in creating widgets')
def menu(self):
# 父类菜单栏模版
# 菜单父容器,参数写窗口
main_menu = Menu(self.root)
# 菜单子容器
menu_contents = Menu(main_menu)
# 菜单项
main_menu.add_cascade(label="目录", menu=menu_contents)
self.root.config(menu=main_menu)
# 删除窗口所有控件
def destroy_all(self):
# 父类清空控件方法
self.user_input.set('')
# winfo_children() 返回一个列表,列表中包含窗口中所有的控件
__list = self.root.winfo_children()
for item in __list:
item.destroy()
def main(self):
self.root.mainloop()
# 完成ascii码转换的类
class ASCII(Home):
def __init__(self):
super(ASCII, self).__init__()
def menu(self):
# 菜单父容器,参数写窗口
main_menu = Menu(self.root)
# 菜单子容器
menu_contents = Menu(main_menu)
# 菜单项
main_menu.add_cascade(label="目录", menu=menu_contents)
menu_contents.add_command(label="ASCII转换工具", command=self.ascii_window)
self.root.config(menu=main_menu)
def ascii_window(self):
self.__main_window()
# 主要部件
def __main_window(self):
self.destroy_all()
self.__menu_ascii()
Label(self.root, text='输入您想要转换为ASCII码的字符').pack()
self.user_input_part = Entry(self.root, textvariable=self.user_input)
self.user_input_part.pack()
Button(self.root, text='转换', command=self.__change).pack()
Button(self.root, text='复制', command=self.__copy).pack()
# 清空输入框的按钮
Button(self.root, text='清空', command=self.__clear).pack()
logging.info('main_window - ascii - succeed in creating widgets')
def __menu_ascii(self):
# 菜单父容器,参数写窗口
main_menu = Menu(self.root)
# 菜单子容器
menu_contents = Menu(main_menu)
# 菜单项
main_menu.add_cascade(label="目录", menu=menu_contents)
menu_contents.add_command(label="字符转换ASCII", command=self.ascii_window)
menu_contents.add_command(label="ASCII转换字符", command=self.__reverse_window)
menu_contents.add_command(label="主页", command=self.home)
self.root.config(menu=main_menu)
# 逆向转换的窗口
def __reverse_window(self):
self.destroy_all()
self.__menu_ascii()
Label(self.root, text='输入您想要转换为字符的ASCII码 (逗号,分号,空格隔开)').pack()
self.user_input_part = Entry(self.root, textvariable=self.user_input)
self.user_input_part.pack()
Button(self.root, text='转换', command=self.__change).pack()
Button(self.root, text='复制', command=self.__copy).pack()
# 清空输入框的按钮
Button(self.root, text='清空', command=self.__clear).pack()
logging.info('main_window - ascii - reverse - succeed in creating widgets')
# 改变输入框中的值为ASCII
def __change(self):
data_string = ''
data_list = list(str(self.user_input.get()))
# 格式化输出
for data in data_list:
data_string += f'{str(ord(data))};'
self.user_input.set(data_string)
logging.info('change - succeed in changing self.user_input -> StringVar()')
return self.user_input
# 改变输入框的值为字符
def __change_reverse(self):
data_string = ''
ascii_string = self.user_input.get()
# 将用户输入的ASCII分隔成列表
ascii_list = re.split(r'[;,;, ]', ascii_string)
try:
# 格式化输出
for item in ascii_list:
if item.isdigit():
data_string += f'{chr(int(item))}'
self.user_input.set(data_string)
logging.info('change - reverse - succeed in changing self.user_input -> StringVar()')
except Exception as e:
logging.error(f'change - ASCII is not exist or {e}')
messagebox.showerror('错误!', '请输入正确的ASCII码并用-空格,逗号,分号隔开')
# 复制ASCII
def __copy(self):
copy(self.user_input.get())
logging.info('copy - succeed in copying self.user_input -> StringVar()')
# 清空输入框
def __clear(self):
self.user_input.set('')
logging.info('clear - succeed in clearing self.user_input -> StringVar()')
# 完成进制转换的类
class Conversation(ASCII):
def __init__(self):
super(Conversation, self).__init__()
def menu(self):
# 菜单父容器,参数写窗口
main_menu = Menu(self.root)
# 菜单子容器
menu_contents = Menu(main_menu)
# 菜单项
main_menu.add_cascade(label="目录", menu=menu_contents)
menu_contents.add_command(label="ASCII转换工具", command=self.ascii_window)
menu_contents.add_command(label="进制转换工具", command=self.conversation_window)
self.root.config(menu=main_menu)
def conversation_window(self):
self.__bin_to_dec_window()
# 主要部件
def __bin_to_dec_window(self):
self.destroy_all()
self.__menu_conversation()
Label(self.root, text='输入您想要转换为十进制的二进制数').pack()
self.user_input_part = Entry(self.root, textvariable=self.user_input)
self.user_input_part.pack()
Button(self.root, text='转换', command=self.__change_bin_to_dec).pack()
Button(self.root, text='复制', command=self.__copy).pack()
# 清空输入框的按钮
Button(self.root, text='清空', command=self.__clear).pack()
logging.info('main_window - bin To dec - succeed in creating widgets')
def __menu_conversation(self):
# 菜单父容器,参数写窗口
main_menu = Menu(self.root)
# 菜单子容器
menu_contents = Menu(main_menu)
# 菜单项
main_menu.add_cascade(label="目录", menu=menu_contents)
menu_contents.add_command(label="二进制转十进制", command=self.__bin_to_dec_window)
menu_contents.add_command(label="十进制转二进制", command=self.__dec_to_bin_window)
menu_contents.add_command(label="主页", command=self.home)
self.root.config(menu=main_menu)
# 逆向转换的窗口
def __dec_to_bin_window(self):
self.destroy_all()
self.__menu_conversation()
Label(self.root, text='输入您想要转换为二进制的十进制数').pack()
self.user_input_part = Entry(self.root, textvariable=self.user_input)
self.user_input_part.pack()
Button(self.root, text='转换', command=self.__change_dec_to_bin).pack()
Button(self.root, text='复制', command=self.__copy).pack()
# 清空输入框的按钮
Button(self.root, text='清空', command=self.__clear).pack()
logging.info('main_window - dec to bin - succeed in creating widgets')
# 改变输入框中的值为十进制
def __change_bin_to_dec(self):
try:
data = int(str(self.user_input.get()), 2)
self.user_input.set(str(data))
logging.info('change - succeed in changing self.user_input -> StringVar()')
except ValueError:
messagebox.showerror('错误!', '请输入正确的二进制数')
self.__clear()
logging.error('change - binary is not exist')
return self.user_input
# 改变输入框的值为二进制
def __change_dec_to_bin(self):
try:
data = bin(int(self.user_input.get()))
# 去掉前面的0b
data = data[2:]
self.user_input.set(data)
logging.info('change - dec to bin - succeed in changing self.user_input -> StringVar()')
except ValueError:
messagebox.showerror('错误!', '请输入正确的十进制数')
self.__clear()
logging.error('change - dec to bin - decimal is not exist')
return self.user_input
# 复制二进制数
def __copy(self):
copy(self.user_input.get())
logging.info('copy - succeed in copying self.user_input -> StringVar()')
# 清空输入框
def __clear(self):
self.user_input.set('')
logging.info('clear - succeed in clearing self.user_input -> StringVar()')
# 从最子类实例化
if __name__ == '__main__':
Conversation().home()
mainloop()
-
1, 本代码采用OOP编写,其中父类为Home,ASCII继承Home,Conversation继承ASCII,以此类推
2, 从最小的子类开始实例化,可以享受上面父类重写的所有方法。
3, 想要增加功能只需要继承Conversation类,super()调用Conversation类的__init__函数获得root以及变量,最后重写menu方法,将目录写好即可。4, 图片与py文件必须在统一目录下,如果出现报错,_
“ tkinter.TclError: couldn't open "Pinkpig.PNG": no such file or directory ”
就把这一句{self.root.iconphoto(False, PhotoImage(file='Pinkpig.PNG'))}删除或者注释掉