簡體   English   中英

PyQt4-信號和插槽。 無法將按鈕連接到方法

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

我的目標是一個GUI窗口,提示用戶輸入特定內容。 我需要用戶打開公司徽標.jpg,測試設置圖片和2個.csv文件數據。 然后,我希望他們輸入報告的名稱以及一些測試設置。

我最初的嘗試成功地為每個項目生成了一個帶有按鈕的彈出窗口。 由於我對每個按鈕都有不同的要求,因此我決定返回並分別進行每個信號/插槽組合。 我希望能夠導入圖片和數據並將該內容分配給變量名稱。 不幸的是,在這種當前配置下,我得到的最接近的結果是它彈出一個窗口,希望用戶選擇一個文件,然后再顯示帶有按鈕的另一個窗口...。 。

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

這里是工作的修復: -移動DEF logo_pic出的init -改變時隙/信號以Logo_button = QtGui.QPushButton(名稱[0])Logo_button.clicked.connect(self.logo_pic)

示例代碼存在一些問題,這些問題已在下面的重寫版本中修復。 希望這可以幫助您正確地開始工作。

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