user3914506
user3914506

Reputation: 65

python tkinter to display File and Save

I have created an coding gui,which doesnot show 'File' and 'save' in the Gui Please help me to fix my problem. I have created a Function for File and Save ,still not working!

please help me to rectify my code!

from tkinter import *
import tkinter.messagebox
import tkinter
import tkinter as tki
import tkinter.filedialog as th1
import re

class App(object):

    def __init__(self,root):
        self.root = root

    # create a Frame for the Text and Scrollbar
        txt_frm = tki.Frame(self.root, width=600, height=400)
        txt_frm.pack(fill="both", expand=True)
        # ensure a consistent GUI size
        txt_frm.grid_propagate(False)

    # create first Text label, widget and scrollbar
        self.lbl1 = tki.Label(txt_frm, text="Type")
        self.lbl1.grid(row=0,column=0,padx=2,pady=2)

        self.txt1 = tki.Text(txt_frm, borderwidth=3, relief="sunken", height=4,width=55)
        self.txt1.config(font=("consolas", 12), undo=True, wrap='word')
        self.txt1.grid(row=0, column=1, sticky="nsew", padx=2, pady=2)

        scrollb1 = tki.Scrollbar(txt_frm, command=self.txt1.yview)
        scrollb1.grid(row=0, column=2, sticky='nsew')
        self.txt1['yscrollcommand'] = scrollb1.set
        button = tki.Button(txt_frm,text="Clickone", command = self.retrieve_input)

        button.grid(column=2,row=0)
        button1 = tki.Button(txt_frm,text="Cli", command = self.clearBox)
        button1.grid(column=2,row=0)
    def retrieve_input(self):
        input1 = self.txt1.get("0.0",'end-1c')
        with open('text.txt','a+') as f:
            f.write(input1+'\n')
        f.close()
    def clearBox(self):
        self.txt1.delete('1.0', 'end')#<-0.0/1.0

def file_save():
        f = th1.asksaveasfile(mode='w', defaultextension=".txt")
        if f is None: # asksaveasfile return `None` if dialog closed with "cancel".
            return
        text2save = str(text.get(1.0, END)) 
        a= (f)
        f.write(text2save)
        f.close()


root = tki.Tk()
menubar=Menu(root)
filemenu=Menu(menubar,tearoff=0)
filemenu.add_command(label="Save", command=file_save)        

app = App(root)
root.mainloop()

Please help!Answers will be appreciated!

Upvotes: 0

Views: 1005

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

You aren't adding the menubar to the window. Add this after you create the menubar.

root.configure(menu=menubar)

You then also have to add the file menu to the menubar:

menubar.add_cascade(label="File", menu=filemenu)

Upvotes: 2

Related Questions