tkinter开发模板

import tkinter as tk


class MyToplevel(tk.Toplevel):
    '''
    继承该类实现自己的方法。注意判读此窗口的唯一,参考示例
    class Top(MyToplevel):
    def __init__(self,master):
        super(Top, self).__init__(master)
        self.geometry("600x700+300+100")
        self['bg']='blue'

        self.btn=tk.Button(self,text='click')
        self.btn.pack()
if __name__ == '__main__':
    root = tk.Tk()
    root.geometry("500x300")
    def fun():
        if not Top.intance:
            Top(root)
        else:
            # 将窗口最小化还原
            if not Top.intance.winfo_exists():
                Top.intance=None
                Top(root)
            else:
                Top.intance.deiconify()
    tk.Button(root, text='ui', command=fun).pack(anchor='center')

    root.mainloop()

    '''
    intance = None
    def __new__(cls, *args, **kwargs):
        if not cls.intance:
            cls.intance = super().__new__(cls)
            return cls.intance

    def __init__(self, master=None):
        if hasattr(self, 'initialized'):
            return
        self.initialized = True
        super(MyToplevel, self).__init__(master)
        self.geometry('400x400+300+100')
        self.configure(bg='cyan')
        self.protocol("WM_DELETE_WINDOW", self.on_close)



    def on_close(self):
        self.destroy()
        MyToplevel.intance = None



class DragFrame(tk.Frame):
    '''
    class MyFrame(DragFrame):
    def __init__(self,master,**kwargs):
        super(MyFrame, self).__init__(master,**kwargs)

        self.btn=tk.Button(self,text='btn',command=self.fun)
        self.btn.place(x=5,y=5)

    def fun(self):
        print('hello')


root = tk.Tk()
root.geometry("800x600+400+100")
root.config(bg='cyan')

frame2 =MyFrame(root, width=200, height=200, bg='black')
frame2.place(x=10, y=10)

root.mainloop()

    '''
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.start_x = 0
        self.start_y = 0
        self.bind('', self.on_press)
        self.bind('', self.on_drag)


    def on_press(self, event):
        self.start_x = event.x
        self.start_y = event.y

    def on_drag(self, event):
        dx = event.x - self.start_x
        dy = event.y - self.start_y
        self.place(x=self.winfo_x() + dx, y=self.winfo_y() + dy)



#菜单模板

import tkinter as tk


def open_file(file_path):
  print('打开文件',file_path)

def close_file(file_path):
    print("关闭文件:", file_path)

def set_file(file_path):
    print("设置文件:", file_path)

def change_appearance(appearance_type):
    print("更改外观:", appearance_type)

def quick_set(setting_type):
    print("快速设置:", setting_type)

def create_menu_bar(root: tk.Tk, file_path, appearance_type, setting_type) -> tk.Menu:
    menu_bar = tk.Menu(root)

    # 创建文件菜单
    file_menu = tk.Menu(menu_bar)
    file_menu.add_command(label="新建项目")
    file_menu.add_command(label="打开", command=lambda: open_file(file_path))
    file_menu.add_separator()
    file_menu.add_command(label='关闭', command=lambda: close_file(file_path))
    file_menu.add_command(label='设置', command=lambda: set_file(file_path))
    menu_bar.add_cascade(label="文件", menu=file_menu)

    # 创建视图菜单
    view_menu = tk.Menu(menu_bar)
    view_menu.add_command(label='工具窗口')
    view_menu.add_command(label='外观', command=lambda: change_appearance(appearance_type))
    view_menu.add_command(label='快速定义', command=lambda: quick_set(setting_type))
    menu_bar.add_cascade(label="视图", menu=view_menu)

    return menu_bar

#示例
if __name__=='__main__':
    root = tk.Tk()
    root['bg']='blue'

    file_path = "example.txt"
    appearance_type = "dark"
    setting_type = "high"

    menu_bar = create_menu_bar(root, root, appearance_type, setting_type)
    root.config(menu=menu_bar)

    root.mainloop()

#单个子窗口处理
import tkinter as tk

def open_window(root,switch=[False,None]):

    if switch[0]:
        #如果最小化再次显示窗口
        if switch[1].state()=='iconic':
            switch[1].state("normal")
        else:
            switch[1].destroy()
    else:
        toplevel=tk.Toplevel()
        toplevel.protocol("WM_DELETE_WINDOW",lambda :open_window(root))
        switch[1]=toplevel
    switch[0] = not switch[0]



if __name__=='__main__':
    root = tk.Tk()
    root.geometry('600x500+200+100')
    root['bg']='blue'

    btn=tk.Button(root,text='click',command=lambda :open_window(root))
    btn.pack(side=tk.LEFT,padx=100,pady=30)




    root.mainloop()

#定时器传参数

import tkinter as tk

def fun3(m):
    print(m)
    root.after(100,lambda :fun3(m))

def open_window(root):
    root.after(2000,lambda :fun3('hello'))



if __name__=='__main__':
    root = tk.Tk()
    root.geometry('600x500+200+100')
    root['bg']='blue'

    varLabel=tk.StringVar(value='hello world')

    btn=tk.Button(root,text='click',command=lambda :open_window(root))
    btn.pack(side=tk.LEFT,padx=100,pady=30)
    root.mainloop()

你可能感兴趣的:(python,开发语言)