0

嗨,我是 Tkinter 编程新手。我已经编写了这个程序,它正在制作中。我创建了一个名为的类IngredientAdder(),并且在该类中的方法下def steps_box我有一个变量self.entry2,它调用文本小部件。当我运行程序时,文本框工作正常。但是,在我的方法下,当我按下方法中的按钮时,def save_recipie我编写了它以打印到终端窗口(words.get 对应于用户在名为 self.entry2 的文本框中输入的文本)。但是,当我运行程序并在文本框中输入文本并按下保存按钮时,没有任何内容打印到我的终端窗口中。如何修改我的代码,以便将用户在文本框中输入的文本打印到我的终端窗口中?帮助?words.get('1.0', 'end')init.gui

如果您可以在代码中添加注释,那将非常有帮助!谢谢。

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()
4

1 回答 1

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)

将 None 注册到self.entry2. 你应该把

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)

但是,您没有注册self.entry2self.text_entries因为您没有调用您的方法text_box

于 2014-06-06T13:50:16.073 回答