This will probably be an easy one, but I have just started learning python, so please bear with me. The following class is supposed to generate the form fields I want :
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())
Problem is, the file window does not wait for the button to be visible, instead, it opens up as soon as I run my main window. The code for the main window is given below:
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_()
What am I doing wrong? How can I make it wait for the button click before opening up the file window? Thanks in advance for the help.
fieldType.clicked.connect(create_file_window())
This creates the file window immediately because create_file_window
is called. You need to pass the function to clicked.connect(...)
without calling it:
fieldType.clicked.connect(create_file_window)
It will then be called when the clicked
event is triggered.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.