【问题标题】:Why dont my buttons work in my pyqt5 GUI?为什么我的按钮在我的 pyqt5 GUI 中不起作用?
【发布时间】:2021-07-21 17:08:59
【问题描述】:

当我制作 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 显示为提供的图片。但是按钮不起作用。我没有收到任何错误消息。他们仍然可以点击,但他们不运行我提供的命令。谁能帮帮我。

【问题讨论】:

  • 我想“buttom”和“bottum”的意思是“bottom”。只是为了澄清,说你有一个名为“bottum_buttons 在底部创建按钮”的文件有点绕口令:-)
  • 感谢您的提醒。是的,你说得对,英语也不是我的母语,所以有时我会犯语法错误,哎呀……是的,这是一个真正的 tong twister。也许将其重构为其他东西

标签: python oop user-interface pyqt pyqt5


【解决方案1】:

我不知道Advanced_window.advancedwindows() 应该做什么,但是您的取消按钮连接到bottom_buttons.close,而不是连接到main_screen.close,我认为这是您想要的。由于bottom_buttons 事先不知道应该关闭哪个窗口,因此您无法真正将按钮连接到预定义小部件的关闭方法。但是,您可以做的是使用 self.window().close() 代替它会关闭具有窗口的 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

【讨论】:

  • 感谢为我工作的人。抱歉英语不好和代码不好。我还在学习很多关于编码的知识。感谢您抽出宝贵的时间。关于 Advanced_window.advancedwindow,这意味着当单击按钮时,它需要创建一个 advancedwindow 类的实例
  • 它是 Advanced_window.py 中的一个类。但我现在也不知道该怎么做
猜你喜欢
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 2016-04-19
  • 1970-01-01
  • 2010-10-30
  • 2022-01-23
  • 1970-01-01
相关资源
最近更新 更多