Home > front end >  The problem of assigning class to an object
The problem of assigning class to an object

Time:09-17

import sys
from PyQt5.QtWidgets import *
from PyQt5.QAxContainer import *
from PyQt5.QtCore import *
import time
sys.stdout = open('A_cook_list.txt', 'a')
TR_REQ_TIME_INTERVAL = 1

class Systembox(QAxWidget):
    def __init__(self):
        super().__init__()
        self._create_sysembox_instance()
        self._set_signal_slots()
        self.c = 0
        self.result = []
    def _create_systembox_instance(self):
        skip...
    def _set_signal_slots(self):
        skip...
    def comm_connect(self):
        skip...
    def _event_connect(self, err_code):
        skip...
    def set_input_value(self, id, value):
        skip...
    def comm_rq_data(self, skip...
        skip...
    def _comm_get_data(self, skip...
        skip...
    def _get_repeat_cnt(self, skip...
        skip...
    def _receive_tr_data(self, skip...
        skip...
    def _AWS10000(self, rqname, trcode):
        cnn = self._get_repeat_cnt(trcode, rqname)
        for i in range(cnn):
            date = self._comm_get_data(trcode, rqname, i, "DATA")
            self.c = self.c   1
            if self.c > 299:
                systembox.remained_data = False
                self.c = self.c - 300
            if len(self.result) < 300:
                self.result.append(date)
            if len(self.result) >= 300:
                self.print_result(self.result)
                self.result = []
    def print_result(self, result):
        print(result)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    systembox = Systembox()
    Systembox.comm_connect()
    while True:
        skip...
        while systembox.remained_data == True:
            skip...
sys.stdout.close()

The above code runs fine. But, I want to output the final value of the above code in a different way. Therefore, I changed the code as below


    skip...
    def print_result(self, result):
        return result

aaa = Systembox()
aaa.print_result(result)
print(aaa.result)

if __name__ == "__main__":
   skip...

but no output.

I want to control the output outside of class Systmebox. because, I'm trying to put the output into a pandas DataFrame. I think pandas DataFrame should be written in another module.

An important thing is, I want to output this code outside the class Systmebox.

For too long, I have spent time trying to solve this problem. please help a lot.

CodePudding user response:

Since what your print_result() does is to return result value (which then will be printed). I think it is better to remove it and fix your code a bit

class Systembox(QAxWidget):
    # some code

    def _AWS10000(self, rqname, trcode):
        cnn = self._get_repeat_cnt(trcode, rqname)
        for i in range(cnn):
            date = self._comm_get_data(trcode, rqname, i, "DATA")
            self.c = self.c   1
            if self.c > 299:
                systembox.remained_data = False
                self.c = self.c - 300
            if len(self.result) < 300:
                self.result.append(date)
            if len(self.result) >= 300:
                print(self.result)         # (1)
                self.result = []
#   def print_result(self, result):        # remove this
#       print(result)                      #

When call:

aaa = Systembox()
print(aaa.result)        # (2) if you really need to know its status
                         # but it should be `[]` because `aaa` was just created
                         # with empty `result`, as defined in `__init__()`

Remember that (1) only be executed when len(self.result) >= 300. Your initial result is empty so it may take a few actions to output something.

  • Related