简体   繁体   English

PyQt4-信号和插槽。 无法将按钮连接到方法

[英]PyQt4 - signals and slots. Trouble connecting button to a method

My goal is a GUI window that prompts the user for specific stuff. 我的目标是一个GUI窗口,提示用户输入特定内容。 I need the user to open the company logo .jpg, a picture of the test setup, and 2 .csv files of data. 我需要用户打开公司徽标.jpg,测试设置图片和2个.csv文件数据。 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) 这里是工作的修复: -移动DEF logo_pic出的init -改变时隙/信号以Logo_button = QtGui.QPushButton(名称[0])Logo_button.clicked.connect(self.logo_pic)

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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM