Posts: 6
Threads: 2
Joined: Jul 2018
I've saved the menu details in a dictionary, then I was trying to call it to create menu by using loop but its not working, can any body here help to find the solution and here is my code from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import numpy as np
file_menu_dict = {'Open' : 'self.open_file()' ,
'Save' : '',
'Save as': '',
'Print' : '',
'Close' : '',
'Exit' : '',
}
class My_actions():
def __init__(self):#, **kwds):
pass
def open_file(self ):
file_name = filedialog.askopenfilename(filetypes=(("PNG","*.png"),("Gif","*.gif"),("All files","*.*")))
image_file = open(file_name).read()
print(image_file)
def all_menu(self, main_menu , all_menu_dict):
#File menu
for i,j in all_menu_dict.items():
main_menu.add_command( label = i, command = lambda : j)
class My_frame(My_actions):#My_actions):
def __init__(self, **kwds):
super().__init__()
#Menu frame start
self.menu_frame = LabelFrame(kwds['root'] )
self.menu_frame.background = 'red'
self.menu_frame.grid(row = 2, column = 0, columnspan=4, sticky= kwds['sticky'])#side = menu_side)
#Menu frame end
def all_menu_frame(self, root):
#menu start here
menubar = Menu(self.menu_frame)
root.config(menu = menubar)
file = Menu(menubar)
menubar.add_cascade(menu=file, label="File")
# Method to call file menu
self.all_menu(file, file_menu_dict)
#menu end here
#Class my frame for both menu & status menu end here
def main():
root = Tk()
window_size_x=0
window_size_y=0
root.geometry("%dx%d+%d+%d" %(root.winfo_screenwidth(), root.winfo_screenheight(), window_size_x, window_size_y))
actions = My_frame( root=root, frame_width=400, frame_height =50, sticky='S')
actions.all_menu_frame(root = root)
root.mainloop()
if __name__ == '__main__' : main()
Posts: 11,890
Threads: 475
Joined: Sep 2016
Please elaborate on 'not working'.
But a quick look, I don't see where you ever reference file_menu_dict after definition.
Posts: 6
Threads: 2
Joined: Jul 2018
Aug-01-2018, 09:27 AM
(This post was last modified: Aug-01-2018, 09:28 AM by yamoon.)
(Aug-01-2018, 09:10 AM)Larz60+ Wrote: Please elaborate on 'not working'.
But a quick look, I don't see where you ever reference file_menu_dict after definition. kindly check line 48 & 49
Posts: 11,890
Threads: 475
Joined: Sep 2016
ok, must of has a typo in my search.
It works when I try it, click on file and options are there.
Posts: 6
Threads: 2
Joined: Jul 2018
(Aug-01-2018, 09:32 AM)Larz60+ Wrote: ok, must of has a typo in my search.
It works when I try it, click on file and options are there.
Click on Open in file menu it should open dialog file but its not
Posts: 11,890
Threads: 475
Joined: Sep 2016
the open function can't be a string for it to execute it need to be a function pointer, it also needs to
somehow run menu.command, i tried the following, but it also does not work:
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import numpy as np
class My_actions():
def __init__(self):#, **kwds):
pass
def open_file(self ):
print('Trying to open')
file_name = filedialog.askopenfilename(filetypes=(("PNG","*.png"),("Gif","*.gif"),("All files","*.*")))
image_file = open(file_name).read()
print(image_file)
def all_menu(self, main_menu , all_menu_dict):
#File menu
for i,j in all_menu_dict.items():
main_menu.add_command( label = i, command = lambda : j)
class My_frame(My_actions):#My_actions):
def __init__(self, **kwds):
super().__init__()
self.file_menu_dict = {
'Open' : self.open_file,
'Save' : '',
'Save as': '',
'Print' : '',
'Close' : '',
'Exit' : '',
}
#Menu frame start
self.menu_frame = LabelFrame(kwds['root'] )
self.menu_frame.background = 'red'
self.menu_frame.grid(row = 2, column = 0, columnspan=4, sticky= kwds['sticky'])#side = menu_side)
#Menu frame end
def all_menu_frame(self, root):
#menu start here
menubar = Menu(self.menu_frame)
file = Menu(menubar)
menubar.add_cascade(menu=file, label="File")
root.config(menu = menubar)
# Method to call file menu
self.all_menu(file, self.file_menu_dict)
#menu end here
#Class my frame for both menu & status menu end here
def main():
root = Tk()
window_size_x=0
window_size_y=0
root.geometry("%dx%d+%d+%d" %(root.winfo_screenwidth(), root.winfo_screenheight(), window_size_x, window_size_y))
actions = My_frame( root=root, frame_width=400, frame_height =50, sticky='S')
actions.all_menu_frame(root = root)
root.mainloop()
if __name__ == '__main__' :
main() However this does:
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import numpy as np
class My_actions():
def __init__(self):#, **kwds):
pass
def open_file(self ):
print('Trying to open')
file_name = filedialog.askopenfilename(filetypes=(("PNG","*.png"),("Gif","*.gif"),("All files","*.*")))
image_file = open(file_name).read()
print(image_file)
def save_file(self):
pass
def save_as_file(self):
pass
def print_file(self):
pass
def close_file(self):
pass
def prog_exit(self):
pass
# def all_menu(self, main_menu , all_menu_dict):
# #File menu
# for i,j in all_menu_dict.items():
# main_menu.add_command( label = i, command = lambda : j)
class My_frame(My_actions):#My_actions):
def __init__(self, **kwds):
super().__init__()
# self.file_menu_dict = {
# 'Open' : self.open_file,
# 'Save' : '',
# 'Save as': '',
# 'Print' : '',
# 'Close' : '',
# 'Exit' : '',
# }
#Menu frame start
self.menu_frame = LabelFrame(kwds['root'] )
self.menu_frame.background = 'red'
self.menu_frame.grid(row = 2, column = 0, columnspan=4, sticky= kwds['sticky'])#side = menu_side)
#Menu frame end
def all_menu_frame(self, root):
#menu start here
menubar = Menu(self.menu_frame)
file = Menu(menubar)
menubar.add_cascade(menu=file, label="File")
root.config(menu = menubar)
# Method to call file menu
file.add_command(label="Open", command=self.open_file)
file.add_command(label="Save", command=self.save_file)
file.add_command(label="Save as", command=self.save_as_file)
file.add_command(label="Print", command=self.print_file)
file.add_command(label="Close", command=self.close_file)
file.add_command(label="Exit", command=self.prog_exit)
# self.all_menu(file, self.file_menu_dict)
#menu end here
#Class my frame for both menu & status menu end here
def main():
root = Tk()
window_size_x=0
window_size_y=0
root.geometry("%dx%d+%d+%d" %(root.winfo_screenwidth(), root.winfo_screenheight(), window_size_x, window_size_y))
actions = My_frame( root=root, frame_width=400, frame_height =50, sticky='S')
actions.all_menu_frame(root = root)
root.mainloop()
if __name__ == '__main__' :
main()
Posts: 6
Threads: 2
Joined: Jul 2018
(Aug-01-2018, 10:09 AM)Larz60+ Wrote: the open function can't be a string for it to execute it need to be a function pointer, it also needs to
somehow run menu.command, i tried the following, but it also does not work:
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import numpy as np
class My_actions():
def __init__(self):#, **kwds):
pass
def open_file(self ):
print('Trying to open')
file_name = filedialog.askopenfilename(filetypes=(("PNG","*.png"),("Gif","*.gif"),("All files","*.*")))
image_file = open(file_name).read()
print(image_file)
def all_menu(self, main_menu , all_menu_dict):
#File menu
for i,j in all_menu_dict.items():
main_menu.add_command( label = i, command = lambda : j)
class My_frame(My_actions):#My_actions):
def __init__(self, **kwds):
super().__init__()
self.file_menu_dict = {
'Open' : self.open_file,
'Save' : '',
'Save as': '',
'Print' : '',
'Close' : '',
'Exit' : '',
}
#Menu frame start
self.menu_frame = LabelFrame(kwds['root'] )
self.menu_frame.background = 'red'
self.menu_frame.grid(row = 2, column = 0, columnspan=4, sticky= kwds['sticky'])#side = menu_side)
#Menu frame end
def all_menu_frame(self, root):
#menu start here
menubar = Menu(self.menu_frame)
file = Menu(menubar)
menubar.add_cascade(menu=file, label="File")
root.config(menu = menubar)
# Method to call file menu
self.all_menu(file, self.file_menu_dict)
#menu end here
#Class my frame for both menu & status menu end here
def main():
root = Tk()
window_size_x=0
window_size_y=0
root.geometry("%dx%d+%d+%d" %(root.winfo_screenwidth(), root.winfo_screenheight(), window_size_x, window_size_y))
actions = My_frame( root=root, frame_width=400, frame_height =50, sticky='S')
actions.all_menu_frame(root = root)
root.mainloop()
if __name__ == '__main__' :
main() However this does:
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import numpy as np
class My_actions():
def __init__(self):#, **kwds):
pass
def open_file(self ):
print('Trying to open')
file_name = filedialog.askopenfilename(filetypes=(("PNG","*.png"),("Gif","*.gif"),("All files","*.*")))
image_file = open(file_name).read()
print(image_file)
def save_file(self):
pass
def save_as_file(self):
pass
def print_file(self):
pass
def close_file(self):
pass
def prog_exit(self):
pass
# def all_menu(self, main_menu , all_menu_dict):
# #File menu
# for i,j in all_menu_dict.items():
# main_menu.add_command( label = i, command = lambda : j)
class My_frame(My_actions):#My_actions):
def __init__(self, **kwds):
super().__init__()
# self.file_menu_dict = {
# 'Open' : self.open_file,
# 'Save' : '',
# 'Save as': '',
# 'Print' : '',
# 'Close' : '',
# 'Exit' : '',
# }
#Menu frame start
self.menu_frame = LabelFrame(kwds['root'] )
self.menu_frame.background = 'red'
self.menu_frame.grid(row = 2, column = 0, columnspan=4, sticky= kwds['sticky'])#side = menu_side)
#Menu frame end
def all_menu_frame(self, root):
#menu start here
menubar = Menu(self.menu_frame)
file = Menu(menubar)
menubar.add_cascade(menu=file, label="File")
root.config(menu = menubar)
# Method to call file menu
file.add_command(label="Open", command=self.open_file)
file.add_command(label="Save", command=self.save_file)
file.add_command(label="Save as", command=self.save_as_file)
file.add_command(label="Print", command=self.print_file)
file.add_command(label="Close", command=self.close_file)
file.add_command(label="Exit", command=self.prog_exit)
# self.all_menu(file, self.file_menu_dict)
#menu end here
#Class my frame for both menu & status menu end here
def main():
root = Tk()
window_size_x=0
window_size_y=0
root.geometry("%dx%d+%d+%d" %(root.winfo_screenwidth(), root.winfo_screenheight(), window_size_x, window_size_y))
actions = My_frame( root=root, frame_width=400, frame_height =50, sticky='S')
actions.all_menu_frame(root = root)
root.mainloop()
if __name__ == '__main__' :
main() Quote: So if I want to add more items to any menu i can't use for loop , then I need to add it one by one and also I tried to use eval() function but also its not working
Posts: 11,890
Threads: 475
Joined: Sep 2016
I think you can make the dictionary work. Some tweak to the command part of your for i, j loop
Posts: 6
Threads: 2
Joined: Jul 2018
(Aug-01-2018, 10:21 AM)Larz60+ Wrote: I think you can make the dictionary work. Some tweak to the command part of your for i, j loop Quote: Well, do you have any good resources for that issue
Posts: 11,890
Threads: 475
Joined: Sep 2016
working on it. Have to go out will be several hours
|