D
D
Danil2019-08-08 09:33:30
Python
Danil, 2019-08-08 09:33:30

PyQt5 Python, open widget when selecting menubar item, how to implement?

Good afternoon, I decided to switch from easygui (I thought I would learn how to work with gui, but this is my own) to pyqt5
I collected 2 layouts in "designer"
1 - Menu bar (glav.ui), and there is a drop-down list of QAction buttons (for example action_1 , action_2)
2 - A window with a widget (widget.ui) there is just text.
How to open widget.ui when clicking on action2
Help(

app = QtWidgets.QApplication([])
app.setStyleSheet(style)
win = uic.loadUi("glav.ui")
 
win.show()
sys.exit(app.exec())

Styles I think it makes no sense to throw off

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gennady S, 2019-08-08
@flabot

It does not hurt, of course, to bring the code of the forms. You have to give the action a name, and find it through the object received after loading the ui, and connect to the action's signal - the slot:

win = uic.loadUi("main_window.ui")
action = win.findChild(QAction, "your_action_name")
action.triggered.connect(your_object.your_slot)

You can define your window class to define handlers in it:
class MainWindow(QtWidgets.QMainWindow): # ваш базовый класс
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('main_window.ui', self)

        action = self.findChild(QAction, "your_action_name")
        action.triggered.connect(self.your_slot)

        self.show()

    def your_slot(self):
        pass

You can create a new window in the same way:
widget = uic.loadUi("widget.ui")
widget.show()

However, remember that if the widget is created inside a handler slot, the link will be destroyed when the function ends. You can bind the widget to the main window class (self.widget = None), then in the action handler, checking to see if it's created and opened.
PS try to avoid transliteration and "inferior" object names like "glav". This primarily shows the sloppiness of the code and will serve as errors in the future.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question