Python 为什么我能';t从tkinter获取输入值作为文件名….I';我正在用给定的名称保存图像
我试图用用户给定的名称捕获图像,但无法从Tkinter条目中获取值。 这是我的密码。但它表示“Application”对象没有属性“txt”Python 为什么我能';t从tkinter获取输入值作为文件名….I';我正在用给定的名称保存图像,python,tkinter,Python,Tkinter,我试图用用户给定的名称捕获图像,但无法从Tkinter条目中获取值。 这是我的密码。但它表示“Application”对象没有属性“txt” class Application(object): def __init__(self, output_path = "./"): self.vs = cv2.VideoCapture(0) # capture video frames, 0 is your default video camera
class Application(object):
def __init__(self, output_path = "./"):
self.vs = cv2.VideoCapture(0) # capture video frames, 0 is your default video camera
self.output_path = output_path # store output path
self.current_image = None # current image from the camera
self.root = tk.Tk() # initialize root window
self.root.title("Window") # set window title
# self.destructor function gets fired when the window is closed
self.root.protocol('WM_DELETE_WINDOW', self.destructor)
self.panel = tk.Label(self.root) # initialize image panel
self.panel.pack(padx=10, pady=10)
# create a button, that when pressed, will take the current frame and save it to file
lbl=tk.Label(self.root,text="Insert Image Name", font=("Arial Bold",10))
lbl.pack(fill="both", expand=True, padx=5, pady=5)
txt = tk.Entry(self.root,width=10)
txt.pack(fill="both", expand=True, padx=5, pady=5)
btn = tk.Button(self.root, text="Capture", command=self.take_snapshot)
btn.pack(fill="both", expand=True, padx=5, pady=5)
self.video_loop()
def take_snapshot(self):
""" Take snapshot and save it to the file """
index=self.txt.get()
filename =index+".jpg"
p = os.path.join(self.output_path, filename) # construct output path
self.current_image.save(p, "png") # save image as jpeg file
print("[INFO] saved {}".format(filename))
创建
tk.条目时
txt=tk.Entry(self.root,宽度=10)
txt.pack(fill=“both”,expand=True,padx=5,pady=5)
您没有将变量保存到self
,因此无法使用self.txt
访问它。要解决此问题,只需将这两行更改为:
self.txt=tk.Entry(self.root,宽度=10)
self.txt.pack(fill=“both”,expand=True,padx=5,pady=5)
当您创建tk.条目时
txt=tk.Entry(self.root,宽度=10)
txt.pack(fill=“both”,expand=True,padx=5,pady=5)
您没有将变量保存到self
,因此无法使用self.txt
访问它。要解决此问题,只需将这两行更改为:
self.txt=tk.Entry(self.root,宽度=10)
self.txt.pack(fill=“both”,expand=True,padx=5,pady=5)