Search code examples
pythontkintersavetext-widget

Tkinter: How can I save text entered in the Text Widget into a new window?


Hi I am new to programming in Tkinter. I have written this program, which is in the making. I made a class called IngredientAdder() and within that class under the method def steps_box I have a variable self.entry2, which calls upon the Text Widget. When I run the program, the text box works fine. However, under my method def save_recipie I wrote for it to print words.get('1.0', 'end') into the terminal window when I press the button in the init.gui method (words.get corresponds with the text entered by the user in the text box called self.entry2). However, when I run the program and enter text into text box and press the save button, nothing is printed into my terminal window. How can I modify my code so that the user entered text in the text box is printed into my terminal window? Help?

If you could possibly add comments in your code, it would be very helpful!! Thanks.

import Tkinter

class Cookbook(Tkinter.Tk):
    def __init__(self):
        Tkinter.Tk.__init__(self)
        self.title("Cookbook")
        self.geometry("500x500+0+22")

        self.button = []
        for r in range(1):
            for c in range(1):
                b = Button(self).grid(row=r,column=c)
                self.button.append(b)

class Button(Tkinter.Button):
    def __init__(self,parent):
        b = Tkinter.Button.__init__(self, parent, text="Add A New Recipie", height=8, width=15, command=self.make_window)

    def make_window(self):
        popwindow = IngredientAdder()
        popwindow.title_box()
        popwindow.ingredients_box()
        popwindow.steps_box()
        popwindow.init_gui()

class IngredientAdder(Tkinter.Tk):
    def __init__(self):
        Tkinter.Tk.__init__(self)
        self.title("Recipie")
        self.geometry("555x500")

    def title_box(self):
        #Frame for the Title Label and the Title Entry Box
        self.frame1 = Tkinter.Frame(self, height=50, width=550, relief=Tkinter.SUNKEN)
        self.frame1.pack(anchor=Tkinter.NW,side=Tkinter.TOP)
        self.frame1.pack_propagate(False)

        #putting in a Title LABEL and ENTRY BOX
        Tkinter.Label(self.frame1,text="Title:").pack(anchor=Tkinter.NW,side=Tkinter.LEFT)
        self.entry1 = Tkinter.Entry(self.frame1,width=550)
        self.entry1.pack(anchor=Tkinter.NW,side=Tkinter.TOP)

    def ingredients_box(self):
        #Frame for the Ingredients Label and the Ingredients Entry Boxes & Steps label and Steps Textbox
        self.frame2 = Tkinter.Frame(self, height=412,width=550, relief=Tkinter.SUNKEN)
        self.frame2.pack(anchor=Tkinter.NW,side=Tkinter.TOP)
        self.frame2.pack_propagate(False)

        # put an Ingredients label at the top of the window and anchor it there
        ingredients_label = Tkinter.Label(self.frame2,text="Ingredients:").pack(anchor=Tkinter.NW,side=Tkinter.LEFT)   #.grid(row=100, column=0)

    def steps_box(self):
        self.entry2 = Tkinter.Text(self.frame2,width=40,height=33,font="helvetica 12",padx=5,pady=5).pack(anchor=Tkinter.NW,side=Tkinter.RIGHT)

        #putting in an entry box and Steps label for the steps of the recepie
        steps_label = Tkinter.Label(self.frame2,text="Steps:").pack(anchor=Tkinter.NW,side=Tkinter.RIGHT)   #.grid(row=100,column=1)

    def title_save(self):
        self.title_entries.append(self.entry1)

    def text_box(self):
        self.text_entries.append(self.entry2)

    # function to add new ingredients
    def add_ingredient_entry(self):
        entry = Tkinter.Entry(self.frame2)
        entry.pack(anchor=Tkinter.NW,side=Tkinter.TOP)
        self.ingredient_entries.append(entry)

    # get contents of all entry boxes
    def save_recipie(self):
        print("Title:")
        for words in self.title_entries:
            print words.get()
        print("Ingredients:")
        for ingredient in self.ingredient_entries:
            print ingredient.get()
        print("Steps:") 
        for text in self.text_entries:
            print text.get('1.0', 'end')
        print "[Recipie saved]"

    # build initial widgets 
    def init_gui(self):
        # title saved in this array, hopefully...
        self.title_entries = []     

        # this is a list of ingredients entry boxes
        self.ingredient_entries = []

        #this saves the list in this array, hopefully..
        self.text_entries = []

        #Making a frame at the bottom to put both buttons in line
        self.test4 = Tkinter.Frame(self,height=10, relief=Tkinter.SUNKEN)
        self.test4.pack(side=Tkinter.BOTTOM)

        # Put these two buttons at the bottom of the window and anchor them there
        Tkinter.Button(self.test4,text="Save recipe",command=self.save_recipie, width=15).pack(anchor=Tkinter.SE,side=Tkinter.RIGHT)
        Tkinter.Button(self.test4,text="Add ingredient",command=self.add_ingredient_entry, width=15).pack(anchor=Tkinter.NW,side=Tkinter.LEFT)

        # new ingredients will be added between the label and the buttons 
        self.add_ingredient_entry()

top = Cookbook()
top.mainloop()

Solution

  • The code

    def steps_box(self):
        self.entry2 = Tkinter.Text(self.frame2,width=40,height=33,font="helvetica 12",padx=5,pady=5).pack(anchor=Tkinter.NW,side=Tkinter.RIGHT)
    

    register None into self.entry2. You should put

    def steps_box(self):
        self.entry2 = Tkinter.Text(self.frame2, width=40, height=33, font="helvetica 12", padx=5, pady=5)
        self.entry2.pack(anchor=Tkinter.NW, side=Tkinter.RIGHT)
    

    However, you didn't register self.entry2 into self.text_entries because you don't call your method text_box.