Python Forum
[PyQt] change self.var in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] change self.var in a function
#1
import sys,os,re
import numpy as np
import PI_GUI_App_Map as pgam
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, QMessageBox ,QWidget,QGridLayout,QPushButton,QLabel,QTextBrowser,QDesktopWidget
from PyQt5.QtGui import *
from PyQt5 import QtCore
from PyQt5.QtCore import Qt


'''################################CLASS###################################'''
class appmap(pgam.Ui_Form):
    
##########################################################################
    def __init__(self,MainWindow):
        self.setupUi(MainWindow)
        self.main(MainWindow)
        self.xlist=[] '''HERE'''
        self.ylist=[] '''HERE'''
        self.zlist=[] '''HERE'''
##########################################################################
    def main(self,MainWindow):
        MainWindow.setWindowTitle('Approach Curve-Map')

        self.lineEdit_xp.editingFinished.connect(
            lambda: self.list_read(widget=self.lineEdit_xp,tlist=self.xlist)) '''HERE'''
        self.lineEdit_yp.editingFinished.connect(
            lambda: self.list_read(widget=self.lineEdit_yp,tlist=self.ylist)) '''HERE'''
        self.lineEdit_zp.editingFinished.connect(
            lambda: self.list_read(widget=self.lineEdit_zp,tlist=self.zlist)) '''HERE'''

        self.lineEdit_xp.editingFinished.connect(self.sample_stat)
        self.lineEdit_yp.editingFinished.connect(self.sample_stat)
        self.lineEdit_zp.editingFinished.connect(self.sample_stat)

##########################################################################
    def list_read(self,widget,tlist,ty=int): '''HERE'''
        try:
            string=widget.text()
            self.res=[]
            element=string.split(",")
            sets=[]
            num=[]
            for ele in element:
                if '(' in ele:
                    sets.append(ele)           
                else:
                    num.append(ty(ele))
            self.res+=list(map(ty, num))
            for i in sets:
                numset=re.findall(r'[(](.*?)[)]', i)
                rangepara=numset[0].split(' ')
                rangepara=list(map(int, rangepara))
                self.res+=np.arange(rangepara[0],rangepara[1]+1,rangepara[2]).tolist()
            self.res.sort()
            tlist=self.res '''HERE'''
            print(tlist) '''HERE'''
            print(self.xlist) '''HERE'''
            print(self.ylist) '''HERE'''
            print(self.zlist) '''HERE'''
        except KeyboardInterrupt:
            sys.exit(app.exec_())
        except Exception:
            self.errormsg=QMessageBox()
            self.errormsg.setIcon(QMessageBox.Critical)
            self.errormsg.setText('Potentially input error, please check your inputs')
            self.errormsg.setStandardButtons(QMessageBox.Ok)
            self.errormsg.setWindowTitle('Error')
            self.errormsg.show()
        print(tlist)
################################
    def sample_stat(self):
        try:
            num2d=len(self.xlist) * len(self.ylist)
            num3d=len(self.xlist) * len(self.ylist) * len(self.zlist)
            print(len(self.xlist) , len(self.ylist),len(self.zlist) )
            self.lineEdit_total_2d.setText(str(num2d))
            self.lineEdit_total_exp.setText(str(num3d))
        except KeyboardInterrupt:
            sys.exit(app.exec_())
        except Exception:
            pass

if __name__=='__main__':
    os.system('python -m PyQt5.uic.pyuic PI_GUI_App_Map.ui -o PI_GUI_App_Map.py')
    app = QApplication(sys.argv)
    win=QWidget()
    ui=appmap(win)
    win.show()
    sys.exit(app.exec_())
All the related parts have '''HERE'''

I initialised three class vars (self.xlist,self.ylist,self.zlist)

they are passed to one function through a para (tlist) from DIFFERENT widget signals
but the para of the function (tlist) does not actually change the vars of the class.

is there a way to?

Thanks!!!
Reply


Messages In This Thread
change self.var in a function - by catlessness - Jul-21-2021, 04:45 PM
RE: change self.var in a function - by Axel_Erfurt - Jul-21-2021, 06:08 PM
RE: change self.var in a function - by deanhystad - Jul-21-2021, 07:36 PM
RE: change self.var in a function - by catlessness - Jul-22-2021, 11:00 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020