简体   繁体   中英

Why dont my buttons work in my pyqt5 GUI?

When I made a GUI i used different classes to construct the main UI Screen. My code has the following structure:

在此处输入图像描述

This is the GUI itself:

用户界面屏幕

The bottum_buttons.py creates the 3 buttons at the buttom. This is the code that is inside bottum_buttons.py:

import Advanced_window
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from datetime import datetime
import calendar
import sys

class bottum_buttons(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        # Create Layout
        self.bottum_box = QHBoxLayout()

        # Creating Buttons
        self.cancel_button = QPushButton("Cancel")
        self.run_button = QPushButton("Run")
        self.advanced_button = QPushButton("Advancend Options")

        self.add_items_to_layout()
        self.create_button_functions()

    def add_items_to_layout(self):
        self.bottum_box.addWidget(self.cancel_button)
        self.bottum_box.addWidget(self.run_button)
        self.bottum_box.addWidget(self.advanced_button)

    def create_button_functions(self):
        self.cancel_button.clicked.connect(self.close)
        self.advanced_button.clicked.connect(Advanced_window.advancedwindows)

    def return_bottum_buttons(self):
        return self.bottum_box

My code that actually constructs the GUI is inside main_screen.py. The following code is inside this file:

from Ui_Elements import option_box
from Ui_Elements import path_box
from Ui_Elements import bottum_buttons
from Ui_Elements import command_output
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from datetime import datetime
import calendar
import sys


class main_screen(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setWindowTitle("Robo Tool")
        self.main_frame = QVBoxLayout()

        # Get UI Elements
        path_ui = path_box.path_box()
        option_ui = option_box.option_box()
        command_ui = command_output.command_box()
        bottum_ui = bottum_buttons.bottum_buttons()

        self.path = path_ui.return_path_box()
        self.option_box = option_ui.return_options_box()
        self.command_output = command_ui.return_command_box()
        self.bottum_buttons = bottum_ui.return_bottum_buttons()

        self.setLayout(self.add_item_to_frame(self.main_frame))

    def add_item_to_frame(self, main_frame):
        main_frame.addLayout(self.path)
        main_frame.addLayout(self.option_box)
        main_frame.addLayout(self.command_output)
        main_frame.addLayout(self.bottum_buttons)
        return main_frame

app = QApplication(sys.argv)
dialog = main_screen()
dialog.show()
app.exec_()

Now the problem is. When i start the main_screen.py the GUI shows up as the picture provided. But the buttons don't work. I dont get any error message. They're still clickable but they dont run the command i provided. Can somebody please help me out.

I don't know what Advanced_window.advancedwindows() is supposed to do, but your cancel button is connected to bottom_buttons.close , not to main_screen.close which I assume is what you want. Since bottom_buttons has no knowledge in advance about which window is supposed to be closed, you can't really connect the button to the close method of a predefined widget. What you could do however is to use self.window().close() instead which would close the next-level ancestor widget of bottom_buttons that has a window. For this, you would need to set the layout of bottom_bottuns to self.bottom_box and add the whole widget to the layout of main_screen rather than just the layout.

This would mean that you would get something like this for bottom_buttons :

class bottum_buttons(QWidget):
    def __init__(self):
        
        .... as before ....

        # set bottom_box as layout of self
        self.setLayout(self.bottom_box)

    ....

    def create_button_functions(self):
        # connect cancel button to close method of top level ancestor widget
        self.cancel_button.clicked.connect(lambda: self.window().close())
        ....
       

And for main_screen :

class main_screen(QDialog):
    def __init__(self):
        .... as before ....

        # set self.bottom_buttons to bottom_buttons widget rather than the layout
        self.bottum_buttons = bottum_ui

        self.setLayout(self.add_item_to_frame(self.main_frame))

    def add_item_to_frame(self, main_frame):
        ...
        # add bottom_buttons widget to the layout.
        main_frame.addWidget(self.bottum_bottons)
        return main_frame

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM