Aug-26-2019, 11:20 PM
I am trying to display streamed video in a GUI I created using PAGE. I can’t get the references to the Class to work. In line 82 I have: self.Frame1.CanvasZ.after(10, video_stream). Python says: AttributeError: 'Toplevel1' object has no attribute 'CanvasZ'. I’ve tried putting self.Frame1.CanvasZ, Frame1.CanvasZ, etc. but nothing works. The video stream code works fine in a standalone app, but doesn't in the class. I've also tried variations with Canvas.create_image (line 127).
Can someone telling me the secret of getting this to work?
Can someone telling me the secret of getting this to work?
#! /usr/bin/env python # -*- coding: utf-8 -*- # # GUI module generated by PAGE version 4.24.1 # in conjunction with Tcl version 8.6 # Aug 13, 2019 01:18:58 PM MDT platform: Windows NT import sys import tkinter as tk import tkinter.ttk as ttk import carCtrl_support import socket import time import cv2 from PIL import ImageTk, Image # Capture from camera cap = cv2.VideoCapture('http://192.168.1.1:8080/?action=stream') def vp_start_gui(): '''Starting point when module is the main routine.''' global val, w, root root = tk.Tk() carCtrl_support.set_Tk_var() top = Toplevel1 (root) carCtrl_support.init(root, top) root.mainloop() w = None def create_Toplevel1(root, *args, **kwargs): '''Starting point when module is imported by another program.''' global w, w_win, rt rt = root w = tk.Toplevel (root) carCtrl_support.set_Tk_var() top = Toplevel1 (w) carCtrl_support.init(w, top, *args, **kwargs) return (w, top) def destroy_Toplevel1(): global w w.destroy() w = None class Toplevel1: def connect (self): """ Make a socket connection to the car and set the data timeout (timeout also forms the step duration) """ global s if self.sockConnected == True: print("sockConnected True") s.shutdown(socket.SHUT_RDWR) s.close() self.connectBtn.configure(text='''Connect''') self.sockConnected = False print("sockConnected " + str(self.sockConnected)) else: s = socket.socket() host = '192.168.1.1' port = 2001 s.connect((host, port)) data = s.recv(32) stringdata = data.decode('utf-8') print("Data: " + stringdata) s.settimeout(0.1) self.sockConnected = True self.connectBtn.configure(text='''Disconnect''') # function for video streaming def video_stream(self): _, frame = cap.read() cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) img = Image.fromarray(cv2image) imgtk = ImageTk.PhotoImage(image=img) self.Frame1.CanvasZ.after(10, video_stream) def __init__(self, top=None): '''This class configures and populates the toplevel window. top is the toplevel containing window.''' _bgcolor = '#d9d9d9' # X11 color: 'gray85' _fgcolor = '#000000' # X11 color: 'black' _compcolor = '#d9d9d9' # X11 color: 'gray85' _ana1color = '#d9d9d9' # X11 color: 'gray85' _ana2color = '#ececec' # Closest X11 color: 'gray92' font9 = "-family {Segoe UI} -size 9 -weight normal -slant " \ "roman -underline 0 -overstrike 0" swVer = "0.1" self.sockConnected = False top.geometry("583x496+2112+204") top.title("Video" + swVer) top.configure(background="#d9d9d9") top.configure(highlightbackground="#d9d9d9") top.configure(highlightcolor="black") self.Frame1 = tk.Frame(top) self.Frame1.place(relx=0.189, rely=0.081, height=280, width=360) self.Frame1.configure(relief='groove') self.Frame1.configure(borderwidth="2") self.Frame1.configure(relief="groove") self.Frame1.configure(background="white") self.Frame1.configure(width=385) self.Frame1.configure(command = self.video_stream()) #self.LMain = tk.Label(top) #self.LMain.place(relx=0.2, rely=0.2) #self.LMain.pack(padx=5, pady=10, side="left") self.CanvasZ = tk.Canvas(self.Frame1) self.CanvasZ.place(relx=0.05, rely=0.05, height=240, width=320) self.CanvasZ.configure(background="#d9d9d9") self.CanvasZ.configure(borderwidth="0") self.CanvasZ.configure(insertbackground="black") self.CanvasZ.configure(relief="ridge") self.CanvasZ.configure(selectbackground="#c4c4c4") self.CanvasZ.configure(selectforeground="black") self.CanvasZ.configure(width=120) self.CanvasZ.create_image(10, 10) self.pix = self.CanvasZ.create_image(10,10) if __name__ == '__main__': vp_start_gui()