Question

My goal is a GUI window that prompts the user for specific stuff. I need the user to open the company logo .jpg, a picture of the test setup, and 2 .csv files of data. Then I want them to enter the name of the report, and some of the test settings.

My initial stab at this successfully generated a pop up window with a button for each of these items. Since I have different requirements for each button, I decided to go back and do each signal / slot combo separately. I want to be able to import pictures and data and assign that stuff to variable names. Unfortunately, in this current configuration, the closest I've gotten is that it pops up a window where the user is expected to select a file, and after that it shows the other window with the button....which doesn't work.

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import * #yes, I know I did this above. 
from PyQt4.QtCore import * #However, when I only do the first one, I get errors. Same with the second way.

class CompiledWindow(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)

        def logo_pic(self):
            global Logo_picture            
            Logo_picture = unicode( QFileDialog.getOpenFileName() )

        self.setWindowTitle('Reasonably named window')
        names = ['Open Logo Picture', 'Open Setup Picture', 'Open first data file', 'Open second data file', 'Enter text about settings', 'Enter other text about settings', 'Enter third setting', 'Enter fourth setting'] 
#this should give you an idea of how many items I need buttons for. I need to open 4 files and have the user enter several bits of text. 
        grid = QtGui.QGridLayout()
        Logo_button = QtGui.QPushButton(names[0])
        self.connect(Logo_button, QtCore.SIGNAL('clicked()'), QtCore.SLOT(logo_pic(self)))        
        grid.addWidget(Logo_button, 0, 0)        
        self.setLayout(grid)       

app = QtGui.QApplication(sys.argv)
cw = CompiledWindow()
cw.show()
sys.exit(app.exec_())

Here was the fix that worked: - moving def logo_pic out of the init - changing the slot / signal to Logo_button = QtGui.QPushButton(names[0]) Logo_button.clicked.connect(self.logo_pic)

Was it helpful?

Solution

There are several problems with the example code, which have all been fixed in the re-written version below. Hopefully this should help to get you started in the right direction.

import sys
from PyQt4 import QtGui, QtCore

class CompiledWindow(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.setWindowTitle('Reasonably named window')
        names = ['Open Logo Picture', 'Open Setup Picture', 'Open first data file', 'Open second data file', 'Enter text about settings', 'Enter other text about settings', 'Enter third setting', 'Enter fourth setting']
        grid = QtGui.QGridLayout(self)
        self.Logo_button = QtGui.QPushButton(names[0], self)
        self.Logo_button.clicked.connect(self.logo_pic)
        grid.addWidget(self.Logo_button, 0, 0)

    def logo_pic(self):
        self.Logo_picture = unicode(QtGui.QFileDialog.getOpenFileName())
        print(self.Logo_picture)

app = QtGui.QApplication(sys.argv)
cw = CompiledWindow()
cw.show()
sys.exit(app.exec_())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top