python中PyQt5中的Bush按钮无法点击,无法调用OnClicke函数

问题描述 投票:0回答:1

我在这里尝试了多种搜索和解决方案,但没有任何方法适合我的情况。我尝试根据之前的这些问题和解决方案进行修复。在我的代码中,我试图使创建的窗口 GUI 中的每个按钮都可以单击来为每个按钮提供特定的功能。在下面的代码中,我尝试在单击按钮时调用特定的例程。不幸的是,执行代码后的按钮不可点击,并且调用的点击方法中没有执行任何操作。


def window():

 # Create the main window and deside the geometric size of this window
    app = QtWidgets.QApplication(sys.argv)
    win = QtWidgets.QMainWindow()
    win.resize(700, 650)


    importBlockChainButton = QtWidgets.QPushButton("CLICK", win)
    importBlockChainButton.setGeometry(20, 165, 400, 40)
    importBlockChainButton.setStyleSheet("background-color: white;"
                                         "border :2.5px solid ;"
                                         "border-top-color : red;"
                                         "border-left-color :red;"
                                         "border-right-color :red;"
                                         "border-bottom-color : red;"
                                         "color: grey")


    win.show()
    sys.exit(app.exec_())

    def importButtonOnClick():

        print("hello")

        """
        # creating progress bar #1
        pbar1 = QtWidgets.QProgressBar(win)
        # setting its geometry
        pbar1.setGeometry(23, 365, 96, 15)
        pbar1.setStyleSheet("color: white;")
        # setting for loop to set value of progress bar
        for i in range(101):
            # The progress for each group of the extracted fingerprints
             """

        # // the code block for each group (methode should be used here).

        """
       # setting value to progress bar
       pbar1.setValue(i)

    # creating progress bar #2
    pbar2 = QtWidgets.QProgressBar(win)
    # setting its geometry
    pbar2.setGeometry(123, 365, 96, 15)
    pbar2.setStyleSheet("color: white;")
    # setting for loop to set value of progress bar
    for i in range(101):
       # The progress for each group of the extracted fingerprints
       """

        # // the code block for each group (methode should be used here).

    """
    # setting value to progress bar
    pbar2.setValue(i)

    # creating progress bar #3
    pbar3 = QtWidgets.QProgressBar(win)
    # setting its geometry
    pbar3.setGeometry(223, 365, 96, 15)
    pbar3.setStyleSheet("color: white;")
    # setting for loop to set value of progress bar
    for i in range(101):
    # The progress for each group of the extracted fingerprints
    """

    # // the code block for each group (methode should be used here).

    """
    # setting value to progress bar
    pbar3.setValue(i)

    # creating progress bar #4
    pbar4 = QtWidgets.QProgressBar(win)
    # setting its geometry
    pbar4.setGeometry(323, 365, 96, 15)
    pbar4.setStyleSheet("color: white;")
    # setting for loop to set value of progress bar
    for i in range(101):
    # The progress for each group of the extracted fingerprints
    """

    # // the code block for each group (methode should be used here).

    """
    # setting value to progress bar
    pbar4.setValue(i)

    # creating progress bar #5
    pbar5 = QtWidgets.QProgressBar(win)
    # setting its geometry
    pbar5.setGeometry(23, 505, 96, 15)
    pbar5.setStyleSheet("color: white;")
    # setting for loop to set value of progress bar
    for i in range(101):
    # The progress for each group of the extracted fingerprints
    """

    # // the code block for each group (methode should be used here).

    """
    # setting value to progress bar
    pbar5.setValue(i)

    # creating progress bar #6
    pbar6 = QtWidgets.QProgressBar(win)
    # setting its geometry
    pbar6.setGeometry(123, 505, 96, 15)
    pbar6.setStyleSheet("color: white;")
    # setting for loop to set value of progress bar
    for i in range(101):
    # The progress for each group of the extracted fingerprints
    """

    # // the code block for each group (methode should be used here).

    """
    # setting value to progress bar
    pbar6.setValue(i)

    # creating progress bar #7
    pbar7 = QtWidgets.QProgressBar(win)
    # setting its geometry
    pbar7.setGeometry(223, 505, 96, 15)
    pbar7.setStyleSheet("color: white;")
    # setting for loop to set value of progress bar
    for i in range(101):
    # The progress for each group of the extracted fingerprints
    """

    # // the code block for each group (methode should be used here).

    """
    # setting value to progress bar
    pbar7.setValue(i)

    # creating progress bar #8
    pbar8 = QtWidgets.QProgressBar(win)
    # setting its geometry
    pbar8.setGeometry(323, 505, 96, 15)
    pbar8.setStyleSheet("color: white;")
    # setting for loop to set value of progress bar
    for i in range(101):
    # The progress for each group of the extracted fingerprints
    """

    # // the code block for each group (methode should be used here).

    """
    # setting value to progress bar
    pbar8.setValue(i)

    """
    importButtonOnClick()

    if importBlockChainButton.clicked:
        importBlockChainButton.clicked.connect(importButtonOnClick)


window()

我正在尝试单击按钮然后执行特定的例程。

python button events pycharm pyqt5
1个回答
0
投票

函数

importButtonOnClick()
定义在
window()
函数内部,但应该定义在外部,这样才能连接到按钮。

希望这对您有帮助。

import sys
from PyQt5 import QtWidgets

def importButtonOnClick():
    print("hello")
    # Your progress bar code should go here

def window():
    app = QtWidgets.QApplication(sys.argv)
    win = QtWidgets.QMainWindow()
    win.resize(700, 650)

    importBlockChainButton = QtWidgets.QPushButton("CLICK", win)
    importBlockChainButton.setGeometry(20, 165, 400, 40)
    importBlockChainButton.setStyleSheet("background-color: white;"
                                         "border :2.5px solid ;"
                                         "border-top-color : red;"
                                         "border-left-color :red;"
                                         "border-right-color :red;"
                                         "border-bottom-color : red;"
                                         "color: grey")

    # Connect the button's clicked signal to the importButtonOnClick function
    importBlockChainButton.clicked.connect(importButtonOnClick)

    win.show()
    sys.exit(app.exec_())

window()
© www.soinside.com 2019 - 2024. All rights reserved.