【问题标题】:How to call a tkinter Class file from another class file?如何从另一个类文件调用 tkinter 类文件?
【发布时间】:2018-05-18 05:15:12
【问题描述】:

我有 2 个单独的文件,每个文件都定义了自己的类。

文件 1:

class AndorCameraSDK:
    def __init__(self):
             self.count = 0 
    def LiveAcquisition(self,nframes,np.ndarray[np.unit16_t,ndim = 3,mode = 'c']image:
             cdef unsigned char * pBuf #This has the data
             #.......Initialisation of parameters..............#
             for i in range(nframes):
                     pBuf = <unsigned char *>calloc(sizeinBytes,sizeof(unsigned char)
                     #-----Storing the data in pBuf into a 3-D array image....#
                     pus_image = <unsigned short*> pBuf
                     for j in range(self.width):
                         pus_image = <unsigned short*> pBuf
                         for k in range(self.height):
                             image[i][j][k] = pus_image[0]
                             pus_image += 1
                         _puc_image += self.stride
  def function1(self):
             #Something else to be done with another function

在文件 2 中

import AndorCameraSDK as andorcamera
class AndorCameraGUI:
     def __init__(self):
           #Making use of Tkinter couple of widgets has been created, which includes a button named LiveImage which calls the function LiveImageGUI.
     def LiveImageGUI (self):
           self.camera = andorcamera()
           #define a 3D array I
           self.camera.LiveAcquisition(nframes,I) #called from File 1
     def LivePlot(self)
          # A function using FigurecanvasTkAgg to display the image processed from LiveAcquisition in the display area defined in the GUI panel

我想要达到的目标:

  1. pBuf 存储到 3D 数组中的 for 循环必须是同一内部线程调用的函数。
  2. 此函数应调用文件 2 中的 LivePlot 函数,以便在 GUI 中显示存储的图像帧。
  3. 这两种情况都应该发生,以便在处理(i+1)st 帧时,显示ith 帧,以免出现时间延迟。

有人可以帮我解决这个问题吗? 非常感谢任何帮助。

【问题讨论】:

    标签: python multithreading tkinter


    【解决方案1】:

    我没有线程化的经验,但我在您的代码中看不到线程化的证据。我已经整理了有关缩进和删除多余代码的代码。左边是说明如何在类之间调用函数的代码。

    from tkinter import *
    
    class AndorCameraSDK():
        def __init__(self, master):
            print('SDK Init') # Show that AndorCameraSDK.__init__ runs
            self.master = master         # Save reference to master
    
        def LiveAcquisition(self):
            print('SDK LiveAcquisition') # Show that AndorCameraSDK.LiveAcquisition runs
            pBuf = 'Some data'       # Dummy data
            self.master.LivePlot() # Call instance of AndorCameraGUI.LivePlot
    
    class AndorCameraGUI():
        def __init__(self):
            print('GUI Init') # Show that AndorCameraGUI.__init__ runs
    
        def LiveImageGUI(self):
            print('GUI LiveImageGUI') # Show that AndorCameraGUI.LiveImageGUI runs
            self.camera = AndorCameraSDK(self) # Create instance of AndorCameraSDK
            self.camera.LiveAcquisition() # Call AndorCameraSDK.LiveAcquisition
    
        def LivePlot(self):
            print('GUI LivePlot') # Show that AndorCameraGUI.LivePlot runs
    
    app = AndorCameraGUI()
    app.LiveImageGUI()  # Instead of pressing a button
    

    【讨论】:

    • 1.是的,这看起来有点符合我的要求。谢谢你。 2. LiveAcquisition() 函数从相机获取图像并存储到一个数组中,对 nframes 执行此操作会产生时间延迟,从而对我的代码造成瓶颈。因此我想处理 (i+1) st 帧并显示第 i框架。 3. 在我的系统中尝试这段代码时,出现如下错误:AndorCameraSDK object has no attribute master.
    • 您是否准确复制了代码?语句self.camera = AndorCameraSDK(self)self 作为参数传递,AndorCameraSDK 将其保存为self.master__init__ 中。应该可以正常工作。否则请粘贴整个错误回溯。
    • 我将完全相同的代码复制到我的笔记本电脑(windows; spyder),它工作得很好。虽然我需要在我的桌面上用 Linux 运行这段代码。我使用 cython 包,因此类 AndorCameraSDK是一个 .pyx 代码,它的定义是: def __cinit__(self,master): self.master = master 这是我与您之前提到的代码的唯一区别。
    • 为此我收到错误:Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“/home/ravindra/anaconda2/lib/python2.7/lib-tk/Tkinter.py” ,第 1541 行,在 call 中返回 self.func(*args) 文件“/home/ravindra/PycharmProjects/LiveMode/GUI.py”,第 278 行,在 getCameraStringGUI self.camera = AndorCameraSDK(self)文件“atcore_py.pyx”,第 77 行,在 AndorCameraDriver.andorCameraSDK3.__cinit__ self.master = master AttributeError: 'AndorCameraDriver.andorCameraSDK3' object has no attribute 'master' Process finished with exit code 0
    • 在多个环境中变得复杂。我想不出这个设置有什么特别的问题。我唯一的建议是在设置后将self.master 的值打印到控制台,然后在使用它之前再次打印它以查看它是否是正确的值。
    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 2021-01-21
    • 2015-02-06
    • 1970-01-01
    • 2020-11-16
    相关资源
    最近更新 更多