我在tkinter中使用菜单小部件创建一个下拉菜单,我需要一些代码,这样当单击其中一个命令时,会找到用户获取该命令所采用的路径。这是因为所需的命令将取决于用户用于获取该命令的路径。如何找到用户所采用的路径?这是我的代码
team1 = 'Lakers'
team2 = 'Celtics'
def makeShot():
print ("Shot was made by")
# Setting up the window and the title etc.
root = Tk()
root.title("Basketball Code")
root.geometry("850x700")
# Lists for the 2 teams.
team1Players = ("Zach Auguste", "Tarik Black", "Anthony Brown", "Jose Calderon", "Luol Deng")
team2Players = ("Ben Bentil", "Avery Bradley", "Jaylen Brown", "Jae Crowder", "Gerald Green")
# Create a popup menu for selecting a team.
teamMenu = Menu(root)
Team2Menu = Menu(root)
Team1Menu = Menu(root)
# Create the 2 options for picking the teams.
teamMenu.add_cascade(label=team1, menu = Team1Menu)
teamMenu.add_cascade(label=team2, menu = Team2Menu)
# Adding the player names to the drop down list
team1Menu = Menu(root)
for i in range(len(team1Players)):
Team1Menu.add_cascade(label=team1Players[i], menu=team1Menu)
# Adding the player names to the drop down list
team2Menu = Menu(root)
for i in range(len(team2Players)):
Team2Menu.add_cascade(label=team2Players[i], menu=team2Menu)
#Adding the options to the players name list.
shotTypeMenu = Menu(root)
team1Menu.add_cascade(label= "3pt", menu=shotTypeMenu)
team1Menu.add_cascade(label= "2pt", menu=shotTypeMenu)
team2Menu.add_cascade(label= "3pt", menu=shotTypeMenu)
team2Menu.add_cascade(label= "2pt", menu=shotTypeMenu)
#Adding the options to the players name list.
shotSuccessMenu = Menu(root)
shotTypeMenu.add_command(label= "Make", command = makeShot)
shotTypeMenu.add_command(label= "Miss", command = None)
def popup(event):
teamMenu.post(event.x_root, event.y_root)
print(event.x,",",event.y)
#Attach popup to frame
root.bind("<Button-3>", popup)
root.mainloop()