Why the variable does not change the value

0

I have the following code where I initialize three variables g_turno , g_sucursal , g_fecha with any value.

These variables should change their value respectively when the following signals are activated:

self.turno_1.currentIndexChanged.connect(lambda:self.turno_online(self.turno_1.currentText()))
self.sucursal.currentIndexChanged.connect(lambda:self.sucursal_online(self.sucursal.currentText()))
self.fecha.dateChanged.connect(self.cambio_fecha)

Later when the signal self.turno_1.currentIndexChanged.connect(lambda:self.turno_online(self.turno_1.currentText()))

is activated, execute the following action:

@QtCore.pyqtSlot(str)
def turno_online(self,str):
    g_turno = str
    if str == 'Mañana':
        self.stack.setCurrentIndex(0)
        editar(self.tabla_1,g_sucursal,g_fecha,g_turno)
    else:
        self.stack.setCurrentIndex(1)
        editar(self.tabla_2,g_sucursal,g_fecha,g_turno)

when I execute the edit function I send the arguments of the previously defined variables as arguments, however the problem is that the variables keep the same value.

This is the complete code:

from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import uic
from PyQt5 import QtCore
from Acces import *
import time
from editar_campos import editar


class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("new_exe.ui",self)

        global g_turno 
        g_turno =  'Matutino'
        global g_sucursal
        g_sucursal =  'Exe 1'
        global g_fecha
        g_fecha = str(time.strftime("%Y-%m-%d"))

        self.fecha.dateChanged.connect(self.cambio_fecha)
        self.turno_1.currentIndexChanged.connect(lambda:self.turno_online(self.turno_1.currentText()))
        self.sucursal.currentIndexChanged.connect(lambda:self.sucursal_online(self.sucursal.currentText()))



    @QtCore.pyqtSlot(QtCore.QDate)
    def cambio_fecha(self,fecha):
        g_fecha = fecha.toPyDate()
        print(g_fecha)

    @QtCore.pyqtSlot(str)
    def turno_online(self,str):
        g_turno = str
        if str == 'Mañana':
            self.stack.setCurrentIndex(0)
            editar(self.tabla_1,g_sucursal,g_fecha,g_turno)
        else:
            self.stack.setCurrentIndex(1)
            editar(self.tabla_2,g_sucursal,g_fecha,g_turno)

    @QtCore.pyqtSlot(str)
    def sucursal_online(self,str):
        g_sucursal = str




app = QApplication([])
p = Principal()
p.show()
app.exec_()
    
asked by Mystic_Force 03.09.2018 в 18:47
source

1 answer

2

Do not use global variables if it is not necessary, and in your case they are not, but in addition you do not know how to use it, when you want to edit a global variable you must indicate that it is, for example in the cambio_fecha() method you you want to assign a new value to g_fecha , then you must do the following:

@QtCore.pyqtSlot(QtCore.QDate)
def cambio_fecha(self,fecha):
    global g_fecha # <--- señalando que modifique la variable global
    g_fecha = fecha.toPyDate()
    print(g_fecha)

But as I point out it is not the best option, right now it is bringing you problems, and imagine how many problems it will bring you in the future. And the worst thing is that beginners think it's something magical and abuse them.

The best option is to use attributes of the class since its scope is of the class and you will be able to access it easily.

On the other hand if you can avoid lambda, avoid them. In your case I think that turno_1 and sucursal have the signal currentTextChanged , so use it directly.

Finally, do not use str as a parameter of a function, str is a function and you are overlapping it, in the case of the decoration pyqtSlot() its use is correct since that decorator requires as data the type of data it will receive the slot.

As you point out that the function% co_of% only prints, I have changed it by editar()

class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("new_exe.ui",self)

        self.g_turno =  'Matutino'
        self.g_sucursal =  'Exe 1'
        self.g_fecha = str(time.strftime("%Y-%m-%d"))

        self.fecha.dateChanged.connect(self.cambio_fecha)
        self.turno_1.currentTextChanged.connect(self.turno_online)
        self.sucursal.currentTextChanged.connect(self.sucursal_online)

    @QtCore.pyqtSlot(QtCore.QDate)
    def cambio_fecha(self,fecha):
        self.g_fecha = fecha.toPyDate()
        print(self.g_fecha)

    @QtCore.pyqtSlot(str)
    def turno_online(self, turno):
        self.g_turno = turno
        if turno == 'Mañana':
            self.stack.setCurrentIndex(0)
            print(self.tabla_1, self.g_sucursal, self.g_fecha, self.g_turno)
        else:
            self.stack.setCurrentIndex(1)
            print(self.tabla_2, self.g_sucursal, self.g_fecha, self.g_turno)

    @QtCore.pyqtSlot(str)
    def sucursal_online(self, sucursal):
        self.g_sucursal = sucursal
    
answered by 03.09.2018 / 20:04
source