I am a newbie at programming and my program is not stellar but hopefully it's ok because I have only been using it for a couple days now.
I am having trouble in my class "Recipie". In this class I am having trouble saving the text in my Entry widget. I know to use the .get() option but when I try to print it, it doesn't (whether it is within that defined method or not). So that is my main concern. I want it to save the text entered as a string when I press the button: b.
My other minor question is, how can I move the label. When I have tried I have used the height and width options, but that just expands the label. I want to move the text to create a title above my Entry boxes. Is label the right widget to use or would it be easier to use a message box widget? So it would look like, for example (but like 8 pixels down and 20 to the right):
ingredients
textbox
button labeled as: add an ingredient
And I am not sure the option .pack(side="...") or .place(anchor="...") are the right options to use for my buttons or entry boxes or labels.
And if you could add comments to your code explaining what you did, that would be so helpful.
import Tkinter
class Cookbook(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.title("Cookbook")
self.geometry("500x500+0+0")
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 = Recipie()
popwindow.name()
popwindow.ingredients()
popwindow.addingredient()
popwindow.savebutton()
popwindow.save()
class Recipie(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.title("New Recipie")
self.geometry("500x500")
def name(self):
name = Tkinter.Label(self, text="Title:")
name.pack() #used to be name.place(anchor="nw")
self.insert_name = Tkinter.Entry(self) #edited with the answer below, used to be insert_name = Tkinter.Entry(self)
self.insert_name.pack() #edited with the answer from below, used to be insert_name.pack()
self.insert_name.focus_set() #edited with the answer from below, used to be insert_name.focus_set()
def ingredients(self):
self.e = Tkinter.Entry(self) #edited with the answer from below, used to be e.get()
self.e.pack() #edited with the answer from below, used to be e.pack()
self.e.focus_set() #edited with the answer from below, used to be e.focus_set()
def addingredient(self):
but = Tkinter.Button(self, text="Add Ingredients", width=15, command=self.ingredients)
but.pack(side="bottom")
def procedure(self):
txt = Tkinter.Label(self, text="List the Steps:")
txt.place(anchor="n")
self.p = Tkinter.Entry(self) #edited with the answer from below, used to be p = Tkinter.Entry(self)
self.p.place(anchor="nw") #edited with the answer from below, used to be p.place(anchor="nw")
self.p.focus_set() #edited with the answer from below, used to be p.focus_set
def savebutton(self):
print self.insert_name.get() #edited with the answer from below
print self.e.get() #edited with the answer from below
print self.p.get() #edited with the answer from below
def save(self):
b = Tkinter.Button(self, text="Save Recipie", width=15,command=self.savebutton)
b.pack()
top = Cookbook()
top.mainloop()