Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

python创建一个带有按钮的窗口,title为菜单实验,左上角有文件按钮,旁边是编辑按钮,点击文件按钮会弹出新建,打开,保存,退出四个按钮,点击每个按钮会弹出标签,每个标签的内…

  1. User

    python创建一个带有按钮的窗口,title为菜单实验,左上角有文件按钮,旁边是编辑按钮,点击文件按钮会弹出新建,打开,保存,退出四个按钮,点击每个按钮会弹出标签,每个标签的内容为按钮的名字,点击编辑按钮,会弹出剪切,复制,粘贴,三个按钮,点击每个按钮会弹出标签,每个标签的内容为按钮的名字

  2. Assistant

    以下是实现上述功能的代码:

    import tkinter as tk
    
    
    class MenuExperiment:
        def __init__(self):
            self.window = tk.Tk()
            self.window.title("菜单实验")
    
            # 创建菜单栏
            menubar = tk.Menu(self.window)
            self.window.config(menu=menubar)
    
            # 文件菜单
            file_menu = tk.Menu(menubar, tearoff=False)
            file_menu.add_command(label="新建", command=self.new_file)
            file_menu.add_command(label="打开", command=self.open_file)
            file_menu.add_command(label="保存", command=self.save_file)
            file_menu.add_separator()
            file_menu.add_command(label="退出", command=self.window.quit)
            menubar.add_cascade(label="文件", menu=file_menu)
    
            # 编辑菜单
            edit_menu = tk.Menu(menubar, tearoff=False)
            edit_menu.add_command(label="剪切", command=self.cut_text)
            edit_menu.add_command(label="复制", command=self.copy_text)
            edit_menu.add_command(label="粘贴", command=self.paste_text)
            menubar.add_cascade(label="编辑", menu=edit_menu)
    
            # 创建按钮
            file_button = tk.Button(self.window, text="文件", command=self.popup_file_menu)
            file_button.pack(side=tk.LEFT, padx=5, pady=5)
    
            edit_button = tk.Button(self.window, text="编辑", command=self.popup_edit_menu)
            edit_button.pack(side=tk.LEFT, padx=5, pady=5)
    
        def popup_file_menu(self):
            file_menu = tk.Menu(self.window, tearoff=False)
            file_menu.add_command(label="新建")
            file_menu.add_command(label="打开")
            file_menu.add_command(label="保存")
            file_menu.add_separator()
            file_menu.add_command(label="退出")
            x, y = self.window.winfo_pointerxy()  # 获取当前鼠标坐标
            file_menu.post(x, y)
    
        def popup_edit_menu(self):
            edit_menu = tk.Menu(self.window, tearoff=False)
            edit_menu.add_command(label="剪切")
            edit_menu.add_command(label="复制")
            edit_menu.add_command(label="粘贴")
            x, y = self.window.winfo_pointerxy()  # 获取当前鼠标坐标
            edit_menu.post(x, y)
    
        def new_file(self):
            self.show_label("新建")
    
        def open_file(self):
            self.show_label("打开")
    
        def save_file(self):
            self.show_label("保存")
    
        def cut_text(self):
            self.show_label("剪切")
    
        def copy_text(self):
            self.show_label("复制")
    
        def paste_text(self):
            self.show_label("粘贴")
    
        def show_label(self, text):
            label = tk.Label(self.window, text=text)
            label.pack()
    
        def run(self):
            self.window.mainloop()
    
    
    if __name__ == '__main__':
        menu_exp = MenuExperiment()
        menu_exp.run()
    

    运行代码后,会弹出一个带有“文件”和“编辑”按钮的窗口。点击“文件”按钮会弹出新建、打开、保存、退出四个选项,点击选项会在窗口中显示对应名称的标签。点击“编辑”按钮会弹出剪切、复制、粘贴三个选项,点击选项同样会在窗口中显示对应名称的标签。