为什么我的按钮在我的 pyqt5 GUI 中不起作用?

Why dont my buttons work in my pyqt5 GUI?

当我制作 GUI 时,我使用了不同的 类 来构造主 UI 屏幕。 我的代码具有以下结构:

这是GUI本身:

bottum_buttons.py 在底部创建了 3 个按钮。这是 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

我实际构造 GUI 的代码在 main_screen.py 中。 此文件中包含以下代码:

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_()

现在问题来了。当我启动 main_screen.py 时,GUI 显示为提供的图片。但是按钮不起作用。我没有收到任何错误消息。它们仍然可以点击,但它们不 运行 我提供的命令。有人可以帮帮我吗

我不知道 Advanced_window.advancedwindows() 应该做什么,但是您的取消按钮连接到 bottom_buttons.close,而不是连接到 main_screen.close,我认为这是您想要的。由于 bottom_buttons 事先不知道应该关闭哪个 window,因此您无法真正将按钮连接到预定义小部件的关闭方法。然而,您可以做的是使用 self.window().close(),这将关闭具有 window 的 bottom_buttons 的下一级祖先小部件。为此,您需要将 bottom_bottuns 的布局设置为 self.bottom_box 并将整个小部件添加到 main_screen 的布局,而不仅仅是布局。

这意味着 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())
        ....
       

对于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