SteveSmith
SteveSmith

Reputation: 19

Converting tkinter buttons into menu items

I am attempting to repurpose some code I have written for a text editor by swapping out buttons for menu items. The current code I have runs without error. However, the menu items are not populated inside the root window. I have provided the code for the original text editor with the buttons and new text editor with the menu items below.

#Editor with buttons

import tkinter as tk
import tkinter.scrolledtext as ts
import os.path

class Editor:
    def __init__(self, win):
        self.root = win
        self.root.title('editor')
        self.root.geometry('500x400+600+200')
        
        #heading
        self.lblHeading = tk.Label(self.root,
                                   text = 'Text editor',
                                   font = ('Times New Roman', 14),
                                   bg = 'white')
        self.lblHeading.place(relx = 0.5, rely = 0.1, anchor = tk.CENTER)
        

        self.textarea = ts.ScrolledText(self.root,
                                        wrap = tk.WORD,
                                        width = 50,
                                        height = 10,
                                        font = ('Times New Roman', 12))
        
        self.textarea.place(relx = 0.5, rely = 0.4, anchor = tk.CENTER)
        
        
        #buttons
        self.Savebtn = tk.Button(self.root, text = 'Save', command = self.save)
        self.Savebtn.place(relx = 0.1, rely = 0.8, anchor = tk.SE)
        
        self.Closebtn = tk.Button(self.root, text = 'Close', command = self.close)
        self.Closebtn.place(relx = 0.2, rely = 0.8, anchor = tk.SE)
        
    def save(self):
        save_path = '../raw/'
        content = self.textarea.get('1.0', tk.END)
        saveas = 'editorText'
        complete_file = os.path.join(save_path, saveas+'.txt')
        saveas = open(complete_file, 'w')
        saveas.write(content)
        
    def close(self):
        self.lblHeading.place_forget()
        self.textarea.place_forget()
        self.Savebtn.place_forget()
        self.Closebtn.place_forget()
        
def main():
    root = tk.Tk()
    Editor(root)
    root.mainloop()
     
if __name__ == '__main__':
    main()

#Editor with menu items

import tkinter as tk
import tkinter.scrolledtext as ts
import os.path

class Editor:
    def __init__(self, win):
        self.root = win
        self.root.title('editor')
        self.root.geometry('500x400+600+200')
        
        #heading
        self.lblHeading = tk.Label(self.root,
                                   text = 'Text editor',
                                   font = ('Times New Roman', 14),
                                   bg = 'white')
        self.lblHeading.place(relx = 0.5, rely = 0.1, anchor = tk.CENTER)
        
        #initializing scrolled textbox
        self.textarea = ts.ScrolledText(self.root,
                                        wrap = tk.WORD,
                                        width = 50,
                                        height = 10,
                                        font = ('Times New Roman', 12))
        
        self.textarea.place(relx = 0.5, rely = 0.4, anchor = tk.CENTER)

        self.menubar = tk.Menu(win)
        # define file menu items
        self.filemenu = tk.Menu(self.menubar, tearoff=0)
        self.filemenu.add_command(label="Save", command=self.save)
        self.filemenu.add_command(label="Close", command=self.close)
        self.menubar.add_cascade(label="File", menu=self.filemenu)

    def save(self):
        save_path = '../raw/'
        content = self.textarea.get('1.0', tk.END)
        saveas = 'editorText'
        complete_file = os.path.join(save_path, saveas+'.txt')
        saveas = open(complete_file, 'w')
        saveas.write(content)
        
    def close(self):
        self.lblHeading.place_forget()
        self.textarea.place_forget()
        
def main():
    root = tk.Tk()
    Editor(root)
    root.mainloop()
    root.config(menu=self.menubar)
     
if __name__ == '__main__':
    main()

Upvotes: 0

Views: 131

Answers (1)

TheLizzard
TheLizzard

Reputation: 7680

You have to tell self.root that there is a menu and that it should display it. Adding self.root.config(menu=self.menubar) to the end of the Editor.__init__ method should do the trick.

Upvotes: 2

Related Questions