Tkinter小工具程序

import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox as mb
import webbrowser


class RosDecrypt(object):
    file_lists = []
    name = ''
    pwd = ''

    def __init__(self):

        self.root = tk.Tk()
        # 获取当前屏幕长宽
        screenwidth = self.root.winfo_screenwidth()
        screenheight = self.root.winfo_screenheight()
        # 设定窗口的大小(长 * 宽)
        width = 240
        height = 180
        # 设置窗口在屏幕居中
        size = "%dx%d+%d+%d" % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.root.geometry(size)

        varName = tk.StringVar()
        varName.set('')
        varPwd = tk.StringVar()
        varPwd.set('')
        # 创建标签
        labelName = tk.Label(self.root, text='用户名:', justify=tkinter.RIGHT, width=80)
        # 将标签放到窗口上
        labelName.place(x=25, y=25, width=80, height=20)
        # 创建文本框,同时设置关联的变量
        self.entryName = tk.Entry(self.root, width=80, textvariable=varName)
        self.entryName.insert('end', 'xiaoradmin')
        self.entryName.place(x=100, y=25, width=80, height=20)
        labelPwd = tk.Label(self.root, text='密  码:', justify=tkinter.RIGHT, width=80)
        labelPwd.place(x=25, y=50, width=80, height=20)
        # 创建密码文本框
        self.entryPwd = tk.Entry(self.root, show='*', width=80, textvariable=varPwd)
        self.entryPwd.place(x=100, y=50, width=80, height=20)
        self.entryPwd.bind(sequence="<Key-Return>", func=self.login)
        buttonOk = tk.Button(self.root, text='登录')
        buttonOk.bind(sequence="<Button-1>", func=self.login)
        buttonOk.place(x=60, y=90, width=50, height=20)
        buttonCancel = tk.Button(self.root, text='取消', command=self.root.quit)
        buttonCancel.place(x=120, y=90, width=50, height=20)
        # 启动消息循环
        self.root.mainloop()

    # 登录按钮事件处理函数
    def login(self, str):
        # 获取用户名和密码
        name = self.entryName.get()
        pwd = self.entryPwd.get()

        if name == 'xiaoradmin' and pwd == '1234567888':
            self.root.withdraw()
            self.Main()

        else:
            tkinter.messagebox.showerror('Error', message='密码错误!')

    def Main(self):
        login_ui = tk.Tk()
        login_ui.title('深圳市小二极客科技有限公司')  # 给窗口设置标题 这里为空
        screenwidth = login_ui.winfo_screenwidth()
        screenheight = login_ui.winfo_screenheight()
        width = 350
        height = 350
        size = "%dx%d+%d+%d" % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        login_ui.geometry(size)
        login_ui.maxsize(350, 350)
        # 创建组件
        top_label = tk.Label(login_ui, text='ROS解密工具', font=('黑体', 15), width=40,
                             height=2)
        self.entey_int = tk.Entry(login_ui, bg='white', font=('黑体', 12))
        self.entey_int.place(width=133, height=20)
        self.text = tk.Text(login_ui, width=16, height=1, font=('黑体', 12))
        label_query = tk.Label(login_ui, text='输入SN查询', width=9)
        bottom_label_1 = tk.Label(login_ui, text='用心做好车 天生为创客', font=('黑体', 10), width=40, height=1)
        button_write = tk.Button(login_ui, text="文件模式")
        button_write.bind(sequence="<Button-1>", func=self.rasp_write)
        button_help = tk.Button(login_ui, text="  帮助  ")
        button_help.bind(sequence="<Button-1>", func=self.help)
        button_exit = tk.Button(login_ui, text="  退出  ", command=login_ui.quit)
        button_query = tk.Button(login_ui, text="手动模式", command=self.query)
        # button_query.bind(sequence="<Button-1>", func=self.query)

        top_label.place(x=-25, y=0)
        label_query.place(x=190, y=60)
        self.entey_int.place(x=150, y=90)
        self.text.place(x=150, y=110)
        button_query.place(x=185, y=140)
        bottom_label_1.place(x=130, y=330)
        button_write.place(x=15, y=95)
        button_help.place(x=240, y=205)
        button_exit.place(x=15, y=205)
        login_ui.mainloop()

    # 查询函数
    @staticmethod
    def query(self):
        txt = self.entey_int.get()
        if len(txt) != 8:
            self.numbe_Error()
        else:
            str_num = RosDecrypt.author(txt)
            print(str_num)
            self.entey_int.delete(0, "end")
            self.text.insert('end', str_num)
            return str_num

    # 文件模式界面
    def rasp_write(self, str):
        write_ui = tk.Tk()
        write_ui.title('文件模式')
        # 设置界面最小且屏幕居中
        screenwidth = write_ui.winfo_screenwidth()
        screenheight = write_ui.winfo_screenheight()
        width = 350
        height = 350
        size = "%dx%d+%d+%d" % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        write_ui.geometry(size)

        top_label = tk.Label(write_ui, text='请点下方按钮进行写入或生成!', font=('黑体', 12))
        top_label2 = tk.Label(write_ui, text='请选择插入SD卡根目录中的sn文件!', font=('黑体', 12))
        button_choose = tk.Button(write_ui, text=" 请选择sn文件 ", font=('黑体', 12))
        button_choose.bind(sequence="<Button-1>", func=self.choose_file)
        self.label_info = tk.Label(write_ui, width=40, height=25, text='', anchor='nw', font=('黑体', 10), justify='left')

        top_label.place(x=30, y=15)
        top_label2.place(x=30, y=45)
        self.label_info.place(x=30, y=105)
        button_choose.place(x=85, y=75)

        write_ui.attributes("-topmost", True)
        write_ui.mainloop()

    # 帮助文档窗口
    def help(self, str):
        win = tk.Tk()
        win.title("help")

        screenwidth = win.winfo_screenwidth()
        screenheight = win.winfo_screenheight()
        width = 350
        height = 350
        size = "%dx%d+%d+%d" % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        win.geometry(size)
        win.maxsize(350, 350)

        top_label = tk.Label(win, text='使用说明', font=('黑体', 14), width=40,
                             height=2)
        bottom_label_1 = tk.Label(win, text='用心做好车 天生为创客', font=('黑体', 10), width=40, height=1)

        text = tk.Text(win, width=35, height=9, font=('微软雅黑', 13))
        button_help = tk.Button(win, text=" 还不明白点我 ")
        button_help.place(x=120, y=280)
        button_help.bind("<Button-1>", self.open_url)
        text.place(x=20, y=70)
        str_help = '树莓派解密步骤:\n    点击文件模式后,请直接选择插入内存卡中boot文件夹里的sn文件即可\n\nJetson Nano解密步骤:\n' \
                   '    点击文件模式前,请把Jetson Nano中的xiaor文件夹里的sn文件拖入win桌面后再点击文件模式,选择sn文件后会生成一个author文件,拖回xiaor文件夹里覆盖即可'
        text.insert(tk.INSERT, str_help)

        top_label.place(x=-5, y=0)
        bottom_label_1.place(x=134, y=330)
        text.place(x=0, y=50)

        win.mainloop()

    def open_url(self, event):
        webbrowser.open("https://www.cnblogs.com/cxstudypython/p/12486645.html", new=0)

    # 文件选择函数
    def choose_file(self, str):
        self.file_lists.append('您选择的文件是:')
        # 弹出弹窗让用户选择文件
        path = tkinter.filedialog.askopenfilename(title='选择sn文件后自动烧录')
        try:
            # 第一种情况:选择的不是SN文件
            if path[-2:] != 'sn':
                self.file_lists.append('错误选择!')
                self.file_lists.append(path)
                self.label_info['text'] = '\n'.join(self.file_lists)
                self.choose_Error()
            # 第二种情况:SN为初始值
            if path[-2:] == 'sn':
                with open(path, 'r') as files:
                    str_num = files.read()
                if str_num == '2d27572e':
                    self.not_Use()
                else:
                    self.file_lists.append(path)
                    self.label_info['text'] = '\n'.join(self.file_lists)
                    way = path[:-3] + '/author'
                    write_num = RosDecrypt.author(str_num)
                    with open(way, 'w') as f:
                        f.write(write_num)
                    self.file_lists.append('写入值为:' + write_num)
                    self.file_lists.append('write successful!!!\n请拔出内存卡')
                    self.label_info['text'] = '\n'.join(self.file_lists)
        except Warning:
            self.unknown_Error()

    # 错误信息窗口
    def choose_Error(self):
        message = tkinter.Tk()
        # 调用withdraw(),让对象不可见
        message.withdraw()
        mb.showinfo('Error', '请选择正确的文件!')

    # 未知错误
    def unknown_Error(self):
        message = tkinter.Tk()
        # 调用withdraw(),让对象不可见
        message.withdraw()
        mb.showinfo('Error', '未知错误!')

    # 内存卡错误信息窗口
    def not_Use(self):
        message = tkinter.Tk()
        # 调用withdraw(),让对象不可见
        message.withdraw()
        mb.showinfo('Error', '内存卡未使用!')

    # 未知错误
    def numbe_Error(self):
        message = tkinter.Tk()
        # 调用withdraw(),让对象不可见
        message.withdraw()
        mb.showinfo('Error', '输入的字符长度不为8!')

    # 加密算法
    @staticmethod
    def author(xin):
        y = xin + 2
        return y


file_write = RosDecrypt()

 

上一篇:R timetk


下一篇:tk 创建含垂直滚动条的frame