大家好,我是Tkinter编程新手。我已经写了这个节目,它正在制作中。我做了一个叫做IngredientAdder()
的类,在这个类的方法def steps_box
下,我有一个变量self.entry2
,它调用文本小部件。当我运行程序时,文本框工作正常。然而,在我的方法def save_recipie
下,我写了它,当我按下init.gui
方法中的按钮时,将words.get('1.0', 'end')
打印到终端窗口。Get对应于用户在名为self.entry2的文本框中输入的文本。然而,当我运行程序并在文本框中输入文本并按下保存按钮时,没有任何内容打印到我的终端窗口。如何修改代码,使用户在文本框中输入的文本打印到终端窗口中?帮助吗?
如果你可以在你的代码中添加注释,这将是非常有帮助的!!谢谢。
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()
代码
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
。你应该把
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.entry2
注册到self.text_entries
,因为您没有调用您的方法text_box
。