-2

I want to call a def that is inside of another def Example:

def 1(a,b):
      Print(a,b)

      Def 2 (c,d):
         Print(c,d)

The thing i want to do is like we call a function from a class like

class().def(variables)

Iwant to do something like the above code Something like

def1().def2(variables)

Thanks :D

Ihave this code

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '2.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

from io import StringIO
from typing import Text
from PyQt5 import QtCore, QtGui, QtWidgets

from PyQt5.QtCore import QTime, QTimer
import sys
import time

from PyQt5 import QtTest




class Ui_MainWindow(object):

    
            
            
        

       
        
    


    def setupUi(self, MainWindow):

       
        
        self.Console = QtWidgets.QTextEdit()
        self.Console.setGeometry(QtCore.QRect(240, 350, 461, 121))
        self.Console.setObjectName("Console")

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton1 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton1.setGeometry(QtCore.QRect(260, 160, 341, 111))
        self.pushButton1.setObjectName("pushButton1")
        
        
        
        
        
        
        
        
        
        
        





        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        
    
        
        def printer (text):
        

            



            sys.stdout = StringIO()
            print(text)
            time.sleep(0.05)
            
            self.Console.setText(sys.stdout.getvalue())

    



    


    
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton1.setText(_translate("MainWindow", "PushButton"))



    
        


    
    
Ui_MainWindow.printer("hi")


QtTest.QTest.qWait(5000)


    







if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I need to call the printer Function but i cant

I tried to make q text object global but i get error The print hi command doesnt work because printer fumc is on another func

kian kian
  • 1
  • 1
  • 4
  • Functions defined inside another function cannot be accessed from the outside, unless a reference to that function is returned or assigned to another variable by the outer function. – quamrana May 26 '21 at 17:31
  • Does this answer your question? [Call Nested Function in Python](https://stackoverflow.com/questions/11154634/call-nested-function-in-python) – wjmolina May 26 '21 at 17:32
  • I'm sorry but the inner function is protected, you need to put it outside. If you want to still group your functions just use a class. – Hemi03 May 26 '21 at 17:34

1 Answers1

0

You cannot call a function inside another function, unless you return it.

What this means, that you cannot do this:

def func1(a,b):
      print(a,b)

      def func2 (c,d):
         print(c,d)

func1().func2()

What you can do however, is this:

def func1(a,b):
    print(a,b)

    def func2 (c,d):
       print(c,d)
    return func2

func2 = func1()
func2()

As to why you would do this is not clear to me, but if you must, this will let you do that.

If possible, I would recommend putting both in a class.

tituszban
  • 4,797
  • 2
  • 19
  • 30