Tkinter菜单和按钮 [英] Tkinter Menu and Buttons

查看:109
本文介绍了Tkinter菜单和按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个类似于任务栏检查器的Tkinter菜单. 因此,如果我转到此菜单并选中一个框,则特定的按钮会出现在我的窗口中,然后用户可以根据需要选择多个按钮.

I'm trying to make a Tkinter menu that is like a taskbar checker. So if I go to this menu and check a box, a specific button then appears on my window, and then the user can select multiple buttons based on what they want.

该程序只是一堆按钮,在我的文本字段中输入文本并单击该按钮后,将启动Web浏览器,并搜索该按钮链接到的网站.

The program is just a bunch of buttons that after entering text in my text field, and clicking the button, a web browser launches with a search of the website that the button is linked to.

如何制作如上所述的菜单?

How can I make a menu like I mentioned above?

我刚刚尝试了基本菜单内容:

I've just tried basic menu stuff:

buttonmenu = Menu(menubar, tearoff=0)
buttonmenu.add_command(label="button1", command=turnbuttononoff)
buttonmenu.add_command(label="button2", command=turnbuttononoff)
buttonmenu.add_command(label="button3", command=turnbuttononoff)
buttonmenu.add_command(label="button4", command=turnbuttononoff)
buttonmenu.add_command(label="button5", command=turnbuttononoff)

这只是创建一个基本菜单.而且,如果我有一个触发按钮打开或关闭的功能,那就太好了.

This just creates a basic menu. And if I could have a function that triggers a button to be turned on or off that would be great.

因此本质上只是一个将按钮从显示切换为不显示的功能

So essentially just a function to swap a button from being shown to not being shown

def turnbuttononoff():
     #togglebutton here

答案: 我制作了每个按钮存储位置的数据字典,然后检查该按钮是否处于活动状态,如果是,则将其关闭,如果处于非活动状态,则将其关闭. 将其设为每个按钮的命令lambda函数即可.

ANSWER: I made a dictionary of the data of where each button was stored, and then checked to see if the button was active, and if it was, turned it off, and if it was inactive, turn it off. Making this a command lambda function for each button works.

def Toggle_Button(myButton):
if myButton.winfo_ismapped()==1:
    myButton.grid_forget()
else:
    myButton.grid(row=gridData[myButton][0],column=gridData[myButton][1])

推荐答案

gridData = {}
gridData[button] = [row,col]


def Toggle_Button(myButton):
    if myButton.winfo_ismapped()==1:
    myButton.grid_forget()
else:
    myButton.grid(row=gridData[myButton][0],column=gridData[myButton][1])

如果网格上已经有按钮,请使用button.grid_info查找所需内容,它会返回一个字典.

If you already have buttons on a grid, use button.grid_info to find what you need, it returns a dictionary.

这篇关于Tkinter菜单和按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆