27
2024
01

tkinter menu creation

import tkinter as tk



win7_window = None  # 用于保存 Win7 窗口的引用

win10_window = None

win11_window = None

win12_window = None





def show_selected_key():

    text_box.delete(1.0, tk.END)

    text_box.insert(tk.END, 'win7 key: 123456')





def close_win7_window():

    global win7_window

    win7_window.destroy()

    win7_window = None  # 当 Win7 窗口关闭时,重置窗口变量


def close_win10_window():

    global win10_window

    win10_window.destroy()

    win10_window = None


def close_win11_window():

    global win11_window

    win11_window.destroy()

    win11_window = None


def close_win12_window():

    global win12_window

    win12_window.destroy()

    win12_window = None



def show_win7_window():

    global win7_window

    if win7_window is None:  # 如果 Win7 窗口尚未打开

        win7_window = tk.Toplevel(root)

        win7_window.geometry("720x180")

        win7_window.title("Keygen")

        win7_window.resizable(False, False)


        global text_box   # 在函数内部使用全局的 text_box 变量

        text_box = tk.Text(win7_window, height=10, width=50)

        text_box.place(relx=0.5, rely=0.5, anchor="center")


        button = tk.Button(win7_window, text="Show Key", command=show_selected_key)

        button.place(relx=0.175, rely=0.5, anchor="center")


        win7_window.protocol("WM_DELETE_WINDOW", close_win7_window)  # 监听窗口关闭事件


def show_win10_window():

    global win10_window

    if win10_window is None:

        win10_window = tk.Toplevel(root)

        key_label = tk.Label(win10_window, text="Win10 Key: 678910")

        key_label.pack()

        win10_window.protocol("WM_DELETE_WINDOW", close_win10_window)


def show_win11_window():

    global win11_window

    if win11_window is None:

        win11_window = tk.Toplevel(root)

        key_label = tk.Label(win11_window, text="Win11 Key: 12345")

        key_label.pack()

        win11_window.protocol("WM_DELETE_WINDOW", close_win11_window)


def show_win12_window():

    global win12_window

    if win12_window is None:

        win12_window = tk.Toplevel(root)

        key_label = tk.Label(win12_window, text="Win12 Key: 678910")

        key_label.pack()

        win12_window.protocol("WM_DELETE_WINDOW", close_win12_window)








root = tk.Tk()


menubar = tk.Menu(root)



filemenu = tk.Menu(menubar, tearoff=0, activebackground='red', activeforeground='yellow',bg='light green')

filemenu.add_command(label="Win7", command=show_win7_window)

filemenu.add_command(label="Win10", command=show_win10_window)



othermenu = tk.Menu(menubar, tearoff=0, activebackground='red', activeforeground='yellow',bg='light green')

othermenu.add_command(label="Win11", command=show_win11_window)

othermenu.add_command(label="Win12", command=show_win12_window)



menubar.add_cascade(label="Open", menu=filemenu)


menubar.add_cascade(label="Other", menu=othermenu)


menubar.add_command(label="Close", command=root.destroy)




root.config(menu=menubar)


root.mainloop()


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。