我的目标是一个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)
答案 0 :(得分:1)
示例代码存在一些问题,这些问题已在下面的重写版本中得到修复。希望这有助于让你开始朝着正确的方向前进。
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_())