当我单击“ Hamstring”按钮时,应该显示选项菜单,并且应该禁用该按钮。当我单击“刷新”按钮时,选项菜单应该会消失,而“筋绳”按钮应回到其正常状态。
我需要它以这种方式工作,因为稍后我将有几个其他带有不同选项菜单的按钮
我可能没有正确编码我的函数。尝试了所有我知道并且可以找到的东西。任何帮助将不胜感激!
import tkinter as tk
from PIL import Image, ImageTk
import yoga_Images
import yogaposes
root = tk.Tk()
yoga_dict2 = {}
for yoga_name2 in yogaposes.hamstringposes:
yoga_dict2[yoga_name2] = ImageTk.PhotoImage(Image.open("./yoga_Images/{}.png".format(yoga_name2)))
variable2 = tk.StringVar(root)
variable2.set(yogaposes.hamstringposes[0])
def change_image2(b):
print(b) # selected option
my_image.config(image=yoga_dict2[b])
def onclickHamstring(args):
if args == 1:
opt2 = tk.OptionMenu(root, variable2, *yogaposes.hamstringposes, command=change_image2)
opt2.config(width=50, font=('Helvetica', 12))
opt2.pack(pady=10)
if (ci2_k['state'] == tk.NORMAL):
ci2_k['state'] = tk.DISABLED
else:
ci2_k['state'] = tk.NORMAL
ci2_k = tk.Button(root, text="Hamstring poses", command=lambda:onclickHamstring(1))
ci2_k .pack(side="left",pady=10)
def normalizebuttons():
ci2_k['state'] = tk.NORMAL
opt2.destroy()
normalizeyogabuttons = tk.Button(root, text="Refresh", command=normalizebuttons)
normalizeyogabuttons.pack(side="left",pady=10)
my_image = tk.Label(root)
my_image.pack(pady=10)
root.mainloop()
跟踪:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/ad/Documents/Python/Project_tkinter/test1.py", line 270, in normalizebuttons
opt2.destroy()
NameError: name 'opt2' is not defined
版本2:
import tkinter as tk
from PIL import Image, ImageTk
import yoga_Images
import yogaposes
root = tk.Tk()
yoga_dict2 = {}
for yoga_name2 in yogaposes.hamstringposes:
yoga_dict2[yoga_name2] = ImageTk.PhotoImage(Image.open("./yoga_Images/{}.png".format(yoga_name2)))
variable2 = tk.StringVar(root)
variable2.set(yogaposes.hamstringposes[0])
def change_image2(b):
print(b) # selected option
my_image.config(image=yoga_dict2[b])
my_image = tk.Label(root)
my_image.grid(pady=20, row=1, column=1)
o = tk.OptionMenu(root, variable2, *yogaposes.hamstringposes, command=change_image2)
def optdrops():
opt2 = o
opt2.config(width=50, font=('Helvetica', 12))
opt2.grid(pady=10, row=1, column=1, sticky='n')
def onclickHamstring(args):
if args == 1:
if (ci2_k['state'] == tk.NORMAL):
ci2_k['state'] = tk.DISABLED
else:
ci2_k['state'] = tk.NORMAL
if args == 2:
if (ci2_k['state'] == tk.DISABLED):
ci2_k['state'] = tk.NORMAL
optdrops()
else:
ci2_k['state'] = tk.DISABLED
o.destroy()
my_image.destroy()
ci2_k = tk.Button(root, text="Hamstring poses", command=lambda:[onclickHamstring(1), optdrops()])
ci2_k.grid(pady=10, row=0, column=1)
normalizeyogabuttons = tk.Button(root, text="Refresh", command=lambda:onclickHamstring(2))
normalizeyogabuttons.grid(pady=10, row=2, column=1)
root.mainloop()