PyQT5:为什么我的文件窗口立即打开?

时间:2019-08-13 07:14:49

标签: python pyqt5

这可能很简单,但是我刚开始学习python,所以请多多包涵。下面的类应该生成我想要的表单字段:

import PyQt5
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QFormLayout, QLabel, QFileDialog, QPushButton


def create_file_window():
    print("file dialog wanted")
    fileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, 'Select Image', '', 'Image Files (*.png *.jpg '
                                                                                  '*.jpeg *.bmp)')
    if fileName:
        print(fileName)


class CreateFormElements(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs):
        super(CreateFormElements, self).__init__(*args, **kwargs)

    def create_form_row(self, label, fieldType, *args, **kwargs):
        layout = QFormLayout()
        layout.addRow(QLabel(label), fieldType)
        self.setLayout(layout)
        # this arguments will tell me if I want a button, and if the button should open a file window
        connectType = kwargs.get('connectType', None)
        _fieldType = kwargs.get('_fieldType', None)
        # if this is a button that should open up a file window, call the create_file_window() function to make a
        # file window. currently no button is visible and the file window is opening up directly.
        if connectType == 'file' and _fieldType == 'button':
            print(fieldType)
            fieldType.setText("Upload new image")
            print('generate file')
            fieldType.clicked.connect(create_file_window())

问题是,文件窗口不等待按钮可见,而是在我运行主窗口后立即打开。主窗口的代码如下:

import sys

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QWidget, QVBoxLayout, QLineEdit, QFormLayout, \
    QFileDialog, QPushButton
from lib.create_form_elements import CreateFormElements


# view
# Subclass QMainWindow to customise your application's main window
class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.myWindow()

    def myWindow(self):
        self.setWindowTitle("My Awesome App")
        sizeObject = QtWidgets.QDesktopWidget().screenGeometry(-1)
        print(" Screen size : " + str(sizeObject.height()) + "x" + str(sizeObject.width()))

        self.width = 600
        self.height = 480
        self.left = 0
        self.top = 0
        self.setGeometry(self.left, self.top, self.width, self.height)
        layout = QVBoxLayout()
        elm = CreateFormElements()
        elm.create_form_row("Object Name: ", QPushButton(), connectType='file', _fieldType='button')
        layout.addWidget(elm)
        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec_()

我在做什么错?如何打开文件窗口之前等待按钮单击? 预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

fieldType.clicked.connect(create_file_window())

由于调用create_file_window,将立即创建文件窗口。您需要将该函数传递给clicked.connect(...) 而无需调用它:

fieldType.clicked.connect(create_file_window)

在触发clicked事件时将调用它。