Python Forum
[Tkinter] [python]Problem in creating menu[/python]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] [python]Problem in creating menu[/python]
#11
This shouldn't be difficult, but I couldn't pin it down.
I did add the following to the code I posted before, to see how the internal dictionary for the menu was constructed, but didn't want to spend more time breaking down the references:
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import numpy as np
import pprint

 
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 pretty(self, mydict, indent=0, fileptr=None):
        for key, value in mydict.items():
            if fileptr:
                fileptr.write(f"{' ' * indent}{key}\n")
            else:
                print(f"{' ' * indent}{key}")
            if isinstance(value, dict):
                pretty(value, indent+1)
            else:
                if fileptr:
                    fileptr.write(f"{' ' * (indent+1)}{value}\n")
                else:
                    print(f"{' ' * (indent+1)}{value}")

    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)

            options = file.config()
            with open('options.txt', 'w') as fp:
                self.pretty(options, indent=4, fileptr=fp)
            # print(json.dumps(options, indent=1))
            # 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()
contents of options.txt file:
Output:
(venv) Larz60p@linux-nnem: forum:$cat options.txt activebackground ('activebackground', 'activeBackground', 'Foreground', <border object: '#ececec'>, <border object: '#ececec'>) activeborderwidth ('activeborderwidth', 'activeBorderWidth', 'BorderWidth', <pixel object: '1'>, <pixel object: '1'>) activeforeground ('activeforeground', 'activeForeground', 'Background', <color object: '#000000'>, <color object: '#000000'>) background ('background', 'background', 'Background', <border object: '#d9d9d9'>, <border object: '#d9d9d9'>) bd ('bd', '-borderwidth') bg ('bg', '-background') borderwidth ('borderwidth', 'borderWidth', 'BorderWidth', <pixel object: '1'>, <pixel object: '1'>) cursor ('cursor', 'cursor', 'Cursor', <cursor object: 'arrow'>, <cursor object: 'arrow'>) disabledforeground ('disabledforeground', 'disabledForeground', 'DisabledForeground', <color object: '#a3a3a3'>, <color object: '#a3a3a3'>) fg ('fg', '-foreground') font ('font', 'font', 'Font', <font object: 'TkMenuFont'>, <font object: 'TkMenuFont'>) foreground ('foreground', 'foreground', 'Foreground', <color object: '#000000'>, <color object: '#000000'>) postcommand ('postcommand', 'postCommand', 'Command', '', '') relief ('relief', 'relief', 'Relief', <index object: 'raised'>, <index object: 'raised'>) selectcolor ('selectcolor', 'selectColor', 'Background', <color object: '#000000'>, <color object: '#000000'>) takefocus ('takefocus', 'takeFocus', 'TakeFocus', '0', '0') tearoff ('tearoff', 'tearOff', 'TearOff', 1, 1) tearoffcommand ('tearoffcommand', 'tearOffCommand', 'TearOffCommand', '', '') title ('title', 'title', 'Title', '', '') type ('type', 'type', 'Type', <index object: 'normal'>, <index object: 'normal'>)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with python raarkil 22 6,693 Apr-30-2021, 01:05 PM
Last Post: raarkil
  I need help on creating a Python Software Chrype 5 4,368 Mar-04-2020, 03:50 PM
Last Post: Chrype
  [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 4,539 Feb-27-2020, 05:17 PM
Last Post: Jan_97
  [Tkinter] macOS Catalina and Python menu issue SouthernYankee 7 6,763 Dec-11-2019, 02:02 AM
Last Post: chrstphrchvz
  Nesting menu buttons tkinter for python Mocap 1 2,695 Jul-18-2019, 11:46 AM
Last Post: metulburr
  Creating An Alert Tone through a python gui on chromium. Fluffyyyzzz 1 1,892 Aug-02-2018, 07:29 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020