zoukankan      html  css  js  c++  java
  • Tkinter 之Menu菜单标签

    一、参数说明

    语法作用
    MenuBar = tk.Menu(window) 创建一个菜单栏
    fileBar = tk.Menu(MenuBar, tearoff=0) 创建一个菜单项,不分窗。
    MenuBar.add_cascade(label="File", menu=fileBar) 在菜单栏添加File菜单
    fileBar.add_command(label="open") 在菜单项中加入子菜单
    fileBar.add_separator() 在菜单项中加入一条分割线
    window.config(menu =MenuBar) 放置菜单栏到主窗口
    fileBar.delete(0) 删除第一个位置菜单项
    MenuBar.add_checkbutton 添加确认按钮

    二、代码示例

    import tkinter as tk
    
    window = tk.Tk()
    # 设置窗口大小
    winWidth = 600
    winHeight = 400
    # 获取屏幕分辨率
    screenWidth = window.winfo_screenwidth()
    screenHeight = window.winfo_screenheight()
    
    x = int((screenWidth - winWidth) / 2)
    y = int((screenHeight - winHeight) / 2)
    
    # 设置主窗口标题
    window.title("Menu菜单参数说明")
    # 设置窗口初始位置在屏幕居中
    window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
    # 设置窗口图标
    window.iconbitmap("./image/icon.ico")
    # 设置窗口宽高固定
    window.resizable(0, 0)
    
    """menu参数.
    
            Valid resource names: activebackground, activeborderwidth,
            activeforeground, background, bd, bg, borderwidth, cursor,
            disabledforeground, fg, font, foreground, postcommand, relief,
            selectcolor, takefocus, tearoff, tearoffcommand, title, type."""
    # 创建菜单栏
    MenuBar = tk.Menu(window)
    # 将菜单栏放到主窗口
    window.config(menu =MenuBar)
    # 创建文件菜单,不显示分窗
    fileBar = tk.Menu(MenuBar, tearoff=0)
    # 添加文件菜单项
    fileBar.add_command(label="open")
    fileBar.add_command(label="save")
    fileBar.add_command(label="save as")
    # 创建分割线
    fileBar.add_separator()
    fileBar.add_command(label="exit", command=window.destroy)
    # 将文件菜单添加到菜单栏
    MenuBar.add_cascade(label="File", menu=fileBar)
    def deleteMenu():
        # 删除第一个位置菜单项
        fileBar.delete(0)
    
    tk.Button(window, text="删除", command=deleteMenu).pack()
    
    window.mainloop()
    

      

    三、效果图

  • 相关阅读:
    用Total Commander for Android管理应用程序
    我的zsh简单设置
    C# Newtonsoft.Json 使用
    Wireshark 抓包 test
    C# 调用API test
    C# 委托 的语法 之一
    C# 对象初始化器 和数组初始化语法
    C 语言 数据类型长度
    vue 使用 test
    test
  • 原文地址:https://www.cnblogs.com/yang-2018/p/11790561.html
Copyright © 2011-2022 走看看