Python Forum
[PyQt] Call a function in FormA from FormB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Call a function in FormA from FormB
#1
Hi everybody.
I 'm looking for a simple example (with SIGNAL, SLOt and Emit()) where we have:
-a main form, say 'FormA' which opens another form, let's say 'FormB'
-FormB has a button which when clicked calls a function in FormA.

If you are aware of some example please post it here.
Reply
#2
The terms you are using don't mean anything to anyone but yourself with the amount of information provided, see the links in my signature for how to ask better questions.
panoss likes this post
Reply
#3
You should provide example code to help people understand your question. At least mention that you are using some version of Qt.
from PySide6 import QtWidgets, QtCore

class FormB(QtWidgets.QWidget):
    form_done = QtCore.Signal(str)

    def __init__(self, *args, steps=5, **kwargs):
        super().__init__(*args, **kwargs)

        self.entry = QtWidgets.QLineEdit("Enter value here")
        self.button = QtWidgets.QPushButton("Done")
        self.button.clicked.connect(self.button_pressed)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.entry)
        layout.addWidget(self.button)

    def button_pressed(self):
        self.hide()
        self.form_done.emit(self.entry.text())

class FormA(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.form_b = None
        self.button = QtWidgets.QPushButton("Draw B")
        self.button.clicked.connect(self.draw_b)
        self.label = QtWidgets.QLabel("I display the result in this label")
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.button)
        layout.addWidget(self.label)

    def draw_b(self):
        if self.form_b is None:
            self.form_b = FormB()
            self.form_b.form_done.connect(self.b_done)
        self.form_b.show()

    def b_done(self, text):
        self.label.setText(text)

def main():
    app = QtWidgets.QApplication([])
    form_a = FormA()
    form_a.show()
    app.exec()

main()
Any python function/method can be bound to a signal, so the concept of Slots is kind of meaningless.
panoss likes this post
Reply
#4
Guys thank you and forgive me for the lack of information.
deanhystad this is EXACTLY what I wanted!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  GUI Problem / call another function / fill QListwidget flash77 5 880 Jul-30-2023, 04:29 PM
Last Post: flash77
  simple tkinter question function call not opening image gr3yali3n 5 3,424 Aug-02-2022, 09:13 PM
Last Post: woooee
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,424 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  [PyQt] call a function with parametrs from another class atlass218 3 4,773 Feb-29-2020, 11:00 AM
Last Post: atlass218
  [Tkinter] Call a function when switching layouts 4096 0 3,534 Sep-22-2019, 07:39 PM
Last Post: 4096
  [PyQt] call a function in another class darktitan 6 14,127 Jun-22-2019, 03:33 AM
Last Post: darktitan

Forum Jump:

User Panel Messages

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