在Python模块中传递变量

时间:2016-11-17 11:56:36

标签: python python-2.7 user-interface opencv

我正在使用Python进行图像处理项目。我需要一个好的GUI,所以我使用PAGE生成了一个GUI。 现在我有三个.py文件:

  1. PAGE生成的GUI类文件。
  2. PAGE生成的GUI支持文件。
  3. 我的图片处理文件。
  4. PAGE生成的GUI支持文件定义了用于存储GUI组件值的全局名称。

    PAGE生成GUISupport.py:

    #! /usr/bin/env python
    #
    # Support module generated by PAGE version 4.8.6
    # In conjunction with Tcl version 8.6
    #    Nov 16, 2016 07:58:07 AM
    
    
    import sys
    
    try:
        from Tkinter import *
    except ImportError:
        from tkinter import *
    
    try:
        import ttk
        py3 = 0
    except ImportError:
        import tkinter.ttk as ttk
        py3 = 1
    
    global frm #this is the global variable
    frm=None
    
    def init(top, gui, *args, **kwargs):
        global w, top_level, root
        w = gui
        top_level = top
        root = top
    
    def destroy_window():
        # Function which closes the window.
        global top_level
        top_level.destroy()
        top_level = None
    
    if __name__ == '__main__':
        import GUI
        GUI.vp_start_gui()
    

    PAGE生成的GUI.py.我分别添加了一个show_frame()函数。根据{{​​3}}信息。

    #! /usr/bin/env python
    #
    # GUI module generated by PAGE version 4.8.6
    # In conjunction with Tcl version 8.6
    #    Nov 16, 2016 07:58:02 AM
    import sys
    import Tkinter as tk
    import Image, ImageTk
    
    try:
        from Tkinter import *
    except ImportError:
        from tkinter import *
    
    try:
        import ttk
        py3 = 0
    except ImportError:
        import tkinter.ttk as ttk
        py3 = 1
    
    import GUISupport
    
    def vp_start_gui():
        '''Starting point when module is the main routine.'''
        global val, w, root
        root = Tk()
        top = MainForm (root)
        GUISupport.init(root, top)
        root.mainloop()
    
    w = None
    def create_MainForm(root, *args, **kwargs):
        '''Starting point when module is imported by another program.'''
        global w, w_win, rt
        rt = root
        w = Toplevel (root)
        top = MainForm (w)
        top.show_frame()
        GUISupport.init(w, top, *args, **kwargs)
        return (w, top)
    
    def destroy_MainForm():
        global w
        w.destroy()
        w = None
    
    
    class MainForm:
        def __init__(self, top=None):
            '''This class configures and populates the toplevel window.
               top is the toplevel containing window.'''
            self._bgcolor = '#d9d9d9'  # X11 color: 'gray85'
            self._fgcolor = '#000000'  # X11 color: 'black'
            self._compcolor = '#d9d9d9' # X11 color: 'gray85'
            self._ana1color = '#d9d9d9' # X11 color: 'gray85' 
            self._ana2color = '#d9d9d9' # X11 color: 'gray85' 
    
    
            top.geometry("850x500+318+153")
            top.title("MainForm")
            top.configure(background="#d9d9d9")
    
    
            self.lblFrame = Label(top)
            self.lblFrame.place(relx=0.01, rely=0.02, height=471, width=764)
            self.lblFrame.configure(background="#d9d9d9")
            self.lblFrame.configure(disabledforeground="#a3a3a3")
            self.lblFrame.configure(foreground="#000000")
            self.lblFrame.configure(text='''Label''')
            self.lblFrame.configure(width=764)
    
        def show_frame(self):
            img = Image.fromarray(GUISupport.frm)
            imgtk = ImageTk.PhotoImage(image=img)
            self.lblFrame.imgtk = imgtk
            self.lblFrame.configure(image=imgtk)
            self.lblFrame.after(10, show_frame) 
    
    
    if __name__ == '__main__':
        create_MainForm(Tk())
    

    这是我使用OpenCV编写的图像处理代码

    import numpy as np
    import cv2
    
    import GUISupport as guis
    
    cam=cv2.VideoCapture(0)
    
    while 1:
        _,f=cam.read()
        cv2.imshow('frame',f)
        guis.frm=cv2.cvtColor(f, cv2.COLOR_BGR2RGBA) #this is the variable passed using GUISupport.py
    
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            cam.release()
            break
    

    我已经运行了这段代码。但是,我收到了这个错误:

    File "C:\Users\user\Documents\Visual Studio 2013\Projects\testforCAmFeed\testforCAmFeed\GUI.py", line 74, in show_frame
        img = Image.fromarray(GUISupport.frm)
    AttributeError: class Image has no attribute 'fromarray'
    

    我需要将视频和其他变量传递给GUI模块,并需要将tkinter GUI控制器数据传递给图像处理模块。

    怎么做?

0 个答案:

没有答案