所以我一直在阅读 getattr,并且想知道相当于什么
example = val1.val2.val3(item)
Run Code Online (Sandbox Code Playgroud)
将会。所以我知道你必须使用嵌套的 getattr 来实现多级别属性
example = getattr(getattr(val1, "val2"), "val3")
Run Code Online (Sandbox Code Playgroud)
但我如何表示传入的对象?
我有一个简单的PyQt4 GUI,用户可以在其中打开一个txt文件,该文件将显示在GUI的QPlainTextEdit小部件中。这是伪代码:
class mainWindow(QtGui.QWidget):
def __init__(self):
super(mainWindow, self).__init__()
self.layout = QtGui.QVBoxLayout()
self.plain = QtGui.QPlainTextEdit()
self.openButton = QtGui.QPushButton("OPEN")
self.layout.addWidget(self.plain)
self.layout.addWidget(self.openButton)
self.openButton.clicked.connect(self.openFile)
def openFile(self):
openFileName = QtGui.QFileDialog.getOpenFileName(None, "Open File","/some/dir/","TXT(*.txt);;AllFiles(*.*)")
openFile = open(openFileName,'r').read()
self.plainTextEdit.appendPlainText(openFile)
Run Code Online (Sandbox Code Playgroud)
因此,我单击“打开”按钮,将弹出QFileDialog,但是如果我在QFileDialog中单击“取消”按钮,则会收到此错误:
IOError: [Errno 2] No such file or directory: PyQt4.QtCore.QString(u'')
Run Code Online (Sandbox Code Playgroud)
作为程序员,我知道可以很容易地忽略此错误,并且不会影响代码的任何操作,但是用户并不知道。有没有办法消除从终端打印中的错误?
因此,例如,我有具有一个GUI label1+ line_edit1,label2+ line_edit2,button1,button2,button3.在正常意义上,代码看起来有点像这样:
class gridlayout_example(QtGui.QWidget):
def __init__(self):
self.grid_layout = QtGui.QGridLayout()
self.label1 = QtGui.QLabel("label1")
self.grid_layout.addWidget(self.label1,0,0,1,3)
self.line_edit1 = QtGui.QLineEdit()
self.grid_layout.addWidget(self.line_edit1,1,0,1,3)
self.label2 = QtGui.QLabel("label2")
self.grid_layout.addWidget(self.label1,2,0,1,3)
self.line_edit2 = QtGui.QLineEdit()
self.grid_layout.addWidget(self.line_edit2,3,0,1,3)
self.button1 = QtGui.QPushButton("button1")
self.button2 = QtGui.QPushButton("button2")
self.button3 = QtGui.QPushButton("button3")
self.grid_layout.addWidget(self.button1, 4,0,1,1)
self.grid_layout.addWidget(self.button2, 4,1,1,1)
self.grid_layout.addWidget(self.button3, 4,2,1,1)
self.setLayout(self.grid_layout)
Run Code Online (Sandbox Code Playgroud)
但有没有办法将label1+ line_edit1和label2+ 结合起来,line_edit2所以它变成了:
[label1
line edit1 ] -> (0,0,1,3)
[label2
line edit2 ] -> (1,0,1,3)
[button1][button2][button3] -> (2,x,1,1)
Run Code Online (Sandbox Code Playgroud)
所以基本上label1 …
我正在尝试将字符串拆分为单个字符.例如temp="hello"进入"h", "e", "l", "l", "o"
我尝试使用IFS,因为这是我在之前的字符串拆分中使用的,并希望保持脚本的一致性.
IFS='' read h e l l o <<<"$temp"不起作用.我究竟做错了什么?
所以假设我有一个清单:
A = [cat, dog, mouse, horse, bird, rabbit]
Run Code Online (Sandbox Code Playgroud)
将用作参考列表和另一个列表:
B = [dog, rabbit, horse, bird, cat, dog]
Run Code Online (Sandbox Code Playgroud)
具有与参考列表完全相同的元素,但顺序不同。我想按照与列表 A 完全相同的顺序对列表 B 进行排序,所以我尝试的是:
for indexA in range(0, len(A)):
for indexB in range(0, len(B)):
if A[indexA] == B[indexB]:
B[indexA], B[indexB] = B[indexB], B[indexA]
Run Code Online (Sandbox Code Playgroud)
但这并不完全有效......解决这个问题的最佳方法是什么?
所以目前在我的GUI中,我有很多字段需要对它们的作用进行一些解释.我想知道如果用户将鼠标悬停在某种小部件上,是否有办法让一个小窗口或部分弹出窗口?
所以我有这个代码:
class mainWindow(QtGui.QWidget):
def __init__(self):
super(mainWindow, self).__init__()
self.layout = QtGui.QVBoxLayout()
self.label1 = QtGui.QLabel()
self.label1.setText("Name")
self.lineEdit1 = QtGui.QLineEdit()
self.label2 = QtGui.QLabel()
self.label2.setText("Age")
self.lineEdit2 = QtGui.QLineEdit()
Run Code Online (Sandbox Code Playgroud)
我想一个小的弹出窗口,显示当我将鼠标悬停在self.lineEdit1和self.lineEdit2不同的消息.有办法吗?