Tkinter Menu and Buttons

2019-09-01 08:20发布

问题:

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.

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?

Edit:

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

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])

回答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])

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