簡體   English   中英

為什么我的按鈕在我的 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 的布局設置為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM