Olá pessoal ... o erro abaixo acontece porque estou tentando associar um ítem de array a um objeto de Menu do Tkinter ...
Tem alguma forma de isso acontecer ou o Python não tem suporte para isso? ... obrigado ...
Obs.: reduzi o código original para este fórum afim de facilitar o entendimento sendo que o mesmo foi testado também.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Já tentei ...
exec ("%s = %s" % (objeto, Opcao [0]))
objeto = locals()[Opcao [0]]
objeto = globals()[Opcao [0]]
objeto.add_cascade (label = objeto, menu = result)
objeto.add_cascade (label = str (objeto), menu = objeto)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$ Traceback (most recent call last):
File "/home/mint/PYTHON/TESTES/TESTES.PY", line 71, in <module>
app = My_Root (root)
File "/home/mint/PYTHON/TESTES/TESTES.PY", line 49, in __init__
objeto = globals()[Opcao [0]]
KeyError: 'Opcao_0'
^C
$
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/usr/bin/python
from tkinter import *
class My_Root (Frame):
def __init__ (self, master):
menubar = Menu (root)
root.config (menu = menubar)
self.master = master
self.root = Toplevel (self.master)
self.root.title ('Python - RHAONE')
self.root.geometry ('300x200')
self.button = Button (self.master, text = 'Exit', command = self.root.quit)
self.button.place (x = 150, y = 100)
Opcao = []
Opcao.append ('Opcao_0')
Opcao.append ('Opcao_1')
Opcao.append ('Opcao_2')
Opcao_0 = Menu (menubar, tearoff = False)
Opcao_0.add_command (label = ' Opcao_0_Ok', command = root.quit)
objeto = globals()[Opcao [0]]
objeto = Menu (menubar, tearoff = False)
objeto.add_cascade (label = str (objeto), menu = str (objeto))
if __name__ == '__main__':
root = Tk ()
app = My_Root (root)
root.mainloop ()