我是编程的新手,我的程序不是很好,但希望它没关系,因为我现在只使用它几天了。
我在课堂上遇到麻烦" Recipie"。在这个课程中,我无法在Entry小部件中保存文本。我知道要使用.get()选项,但是当我尝试打印它时,它不会(无论是否在该定义的方法内)。这是我的主要关注点。我希望它在按下按钮时保存作为字符串输入的文本:b。
我的另一个小问题是,我该如何移动标签。当我尝试过时,我使用了高度和宽度选项,但这只是扩展了标签。我想移动文本以在我的条目框上方创建标题。标签是使用正确的小部件还是更容易使用消息框小部件?例如,它看起来像(但是像8个像素向下,20个向右):
成分<BR/>
文本框
按钮标记为:添加成分
我不确定选项.pack(side =&#34; ...&#34;)或.place(anchor =&#34; ...&#34;)是正确的选择用于我的按钮或输入框或标签。
如果您可以在代码中添加注释来解释您的操作,那将非常有用。
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()
答案 0 :(得分:1)
第1部分......
您当前正在将ingredients
方法中的条目小部件定义为局部变量(即这些变量仅存在于方法内)。要维护对正在创建的条目小部件的引用,可以将其作为实例属性分配给Recipie
对象。
即
e = Tkinter.Entry(self)
e.pack()
e.focus_set()
变为
self.e = Tkinter.Entry(self)
self.e.pack()
self.e.focus_set()
和
print e.get()
变为
print self.e.get()
继续使用Python之前需要阅读一些内容:
第2部分......
因此,在回答有关如何定位标签的问题的第二部分时。看起来你需要改变你打包小部件的方式。如何打包条目小部件的一个例子是一种干净的方式(模仿你的例子的功能)将是:
import Tkinter as tk
class IngredientAdder(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry("500x500+0+0")
self.init_gui()
# function to add new ingredients
def add_ingredient_entry(self):
entry = tk.Entry(self)
entry.pack(side=tk.TOP)
self.ingredient_entries.append(entry)
# get contents of all entry boxes
def save_recipe(self):
for ingredient in self.ingredient_entries:
print ingredient.get()
print "[Recipe saved]"
# build initial widgets
def init_gui(self):
# this is a list of ingredients entry boxes
self.ingredient_entries = []
# put a label at the top of the window and anchor it there
tk.Label(self,text="Ingredients").pack(anchor=tk.N,side=tk.TOP)
# Put these two buttons at the bottom of the window and anchor them there
tk.Button(self,text="Save recipe",command=self.save_recipe).pack(anchor=tk.S,side=tk.BOTTOM)
tk.Button(self,text="Add ingredient",command=self.add_ingredient_entry).pack(anchor=tk.S,side=tk.BOTTOM)
# new ingredients will be added between the label and the buttons
self.add_ingredient_entry()
cookbook = IngredientAdder()
cookbook.mainloop()