W
W
WolfInChains2020-11-10 16:21:49
PyQt
WolfInChains, 2020-11-10 16:21:49

How to pass parameters to a function when clicking on a button, so as not to create many functions?

There is such a code how you can reduce it to 1 function, to which parameters will be passed, so as not to make a separate function for each city. Parameters are self.ui_map.pb_shadow_isles.clicked.connectnot passed, how can I solve this problem, what are the options?

def start_map(self):
    self.ui_map.setupUi(self)
    self.ui_map.pb_shadow_isles.clicked.connect(self.start_city_shadow_isles_info)
    self.ui_map.pb_bilgewater.clicked.connect(self.start_city_bilgewater_info)
    self.show()


def start_city_shadow_isles_info(self):
    self.ui_city_info.setupUi(self, "SHADOW ISLES_BACK", "SHADOW ISLES_BACKGROUND", "SHADOW ISLES_3D")
    self.ui_city_info.pb_cancel_city_info.clicked.connect(self.start_map)

    result = sq.get_city_info(1)
    self.ui_city_info.l_city_name.setText(str(result[1]))
    self.ui_city_info.l_city_s_name.setText(str(result[2]))
    self.ui_city_info.l_city_edit_info_1.setText(str(result[3]))
    self.ui_city_info.l_city_edit_info_2.setText(str(result[4]))
    self.ui_city_info.l_city_edit_info_3.setText(str(result[6]))
    self.ui_city_info.l_city_edit_info_4.setText(str(result[5]))
    self.ui_city_info.l_city_edit_info_5.setText(str(result[7]))
    self.show()


def start_city_bilgewater_info(self):
    self.ui_city_info.setupUi(self, "BILGEWATER_BACK", "BILGEWATER_BACKGROUND", "BILGEWATER_3D")
    self.ui_city_info.pb_cancel_city_info.clicked.connect(self.start_map)

    result = sq.get_city_info(2)
    self.ui_city_info.l_city_name.setText(str(result[1]))
    self.ui_city_info.l_city_s_name.setText(str(result[2]))
    self.ui_city_info.l_city_edit_info_1.setText(str(result[3]))
    self.ui_city_info.l_city_edit_info_2.setText(str(result[4]))
    self.ui_city_info.l_city_edit_info_3.setText(str(result[6]))
    self.ui_city_info.l_city_edit_info_4.setText(str(result[5]))
    self.ui_city_info.l_city_edit_info_5.setText(str(result[7]))
    self.show()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ronald McDonald, 2020-11-10
@WolfInChains

Parameters are not passed to self.ui_map.pb_shadow_isles.clicked.connect

What is it? Use lambda, mine is like this:
self.myAction.triggered.connect(
                        lambda string="": self.someAction(string)
        )

The declaration of someAction: looks like this:
def someAction(self, string):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question