我是靠谱客的博主 温暖电源,最近开发中收集的这篇文章主要介绍Tkinter极简例子——Menu篇Tkinter Menu,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Tkinter Menu

Menu极简例子

from tkinter import *


def hello():
    print('hello')


root = Tk()

menu = Menu(root)

for item in ['python', 'tkinter', 'else']:
    menu.add_command(label=item, command=hello)

root['menu'] = menu

root.mainloop()

下拉菜单

from tkinter import *


def hello():
    print('hello')


root = Tk()

menu = Menu(root)

filemenu = Menu(menu, tearoff=0)

for item in ['python', 'tkinter', 'else']:
    filemenu.add_command(label=item, command=hello)

menu.add_cascade(label='Language', menu=filemenu)

root['menu'] = menu

root.mainloop()

Menu与Checkbutton绑定

from tkinter import *


def printItem():
    print('Python = ', vPython.get())
    print('PHP = ', vPHP.get())
    print('CPP = ', vCPP.get())
    print('C = ', vC.get())
    print('Java = ', vJava.get())
    print('JavaScript = ', vJavaScript.get())
    print('VBScript = ', vVBScript.get())


root = Tk()

menu = Menu(root)

vPython = StringVar()
vPHP = StringVar()
vCPP = StringVar()
vC = StringVar()
vJava = StringVar()
vJavaScript = StringVar()
vVBScript = StringVar()

filemenu = Menu(menu, tearoff=0)

for k, v in {'Python': vPython,
             'PHP': vPHP,
             'CPP': vCPP,
             'C': vC,
             'Java': vJava,
             'JavaScript': vJavaScript,
             'VBScript': vVBScript}.items():
    filemenu.add_checkbutton(label=k, command=printItem, variable=v)

menu.add_cascade(label='Language', menu=filemenu)

root['menu'] = menu

root.mainloop()

Menu与Radiobutton绑定

from tkinter import *


def printItem():
    print('vLang = ', vLang.get())


root = Tk()

menu = Menu(root)

vLang = StringVar()

filemenu = Menu(menu, tearoff=0)

for k in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript']:
    filemenu.add_radiobutton(label=k, command=printItem, variable=vLang)

menu.add_cascade(label='Language', menu=filemenu)

root['menu'] = menu

root.mainloop()

添加分隔符

from tkinter import *


def printItem():
    print('vLang = ', vLang.get())


root = Tk()

menu = Menu(root)

vLang = StringVar()

filemenu = Menu(menu, tearoff=0)

for k in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript']:
    filemenu.add_radiobutton(label=k, command=printItem, variable=vLang)
    filemenu.add_separator()

menu.add_cascade(label='Language', menu=filemenu)

root['menu'] = menu

root.mainloop()

快捷菜单

from tkinter import *


def printItem():
    print('vLang = ', vLang.get())


def popup(event):
    menu.post(event.x_root, event.y_root)


root = Tk()

menu = Menu(root)

vLang = StringVar()

filemenu = Menu(menu, tearoff=0)

for k in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript']:
    filemenu.add_radiobutton(label=k, command=printItem, variable=vLang)
    filemenu.add_separator()

menu.add_cascade(label='Language', menu=filemenu)

root['menu'] = menu

root.bind('<Button-3>', popup)

root.mainloop()

菜单选项操作

from tkinter import *


def printItem():
    print('vLang = ', vLang.get())


def popup(event):
    menu.post(event.x_root, event.y_root)


root = Tk()

menu = Menu(root)

vLang = StringVar()

filemenu = Menu(menu, tearoff=0)

for k in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript']:
    filemenu.add_radiobutton(label=k, command=printItem, variable=vLang)

filemenu.insert_command(1,label = '1000',command = printItem)
filemenu.insert_checkbutton(2,label = '2000',command = printItem)

filemenu.insert_separator(1)

filemenu.insert_radiobutton(3, label= '3000',command = printItem)

filemenu.delete(6, 9)

menu.add_cascade(label='Language', menu=filemenu)

root['menu'] = menu

root.bind('<Button-3>', popup)

root.mainloop()

最后

以上就是温暖电源为你收集整理的Tkinter极简例子——Menu篇Tkinter Menu的全部内容,希望文章能够帮你解决Tkinter极简例子——Menu篇Tkinter Menu所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(29)

评论列表共有 0 条评论

立即
投稿
返回
顶部