我有两种形式(form1.ui& form2.ui)。 form1包含产品列表和新产品的按钮。新产品按钮打开一个新的form2,它有一个editline和一个插入按钮。插入按钮从另一个类(AllMethods类)调用一个方法,该方法将editline内容的内容插入到form1的列表中.....错误是allmethod实例没有属性itemlist(Qlist的名称)
import sys
from PyQt4 import QtGui, uic
class AllMethods:
def addListMethod(self,itm):
itemList.addItem(itm)
#_________FORM2____________
class Form2(QtGui.QMainWindow):
def __init__(self):
super(Form2,self).__init__()
uic.loadUi('Form2.ui',self)
self.show()
self.insertItem.clicked.connect(self.insertItem_clicked)
def insertItem_clicked(self):
obj = AllMethods()
obj.addListMethod(self.itemname.text())
#_________FORM1____________
class Form1(QtGui.QMainWindow):
def __init__(self):
super(Form1,self).__init__()
uic.loadUi('Form1.ui',self)
self.show()
self.newItem.clicked.connect(self.newItem_clicked)
def newItem_clicked(self):
self.form_second = Form2()
self.form_second.show()
#_________________MAIN____________________
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Form1()
sys.exit(app.exec_())
我试着像
那样调用Form1类mainF = Form1()
mainF.itemlist.addItem(itm)
但是另一个form1弹出并在一秒钟内消失,而打开的form1仍然是空的。
实际上这是一个小程序,它说明了我在主程序中的问题。
答案 0 :(得分:0)
感谢@mikuszefski 仅使用两个form1,form2(form2方法添加项目在form1中列出)
import sys
from PyQt4 import QtGui, uic
#_________FORM2____________
class Form2(QtGui.QMainWindow):
def __init__(self,parent):
super(Form2,self).__init__(parent)
uic.loadUi('Form2.ui',self)
self.show()
self.parent = parent
self.insertItem.clicked.connect(self.insertItem_clicked)
def insertItem_clicked(self):
self.parent.itemList.addItem(self.itemname.text())
#_________FORM1____________
class Form1(QtGui.QMainWindow):
def __init__(self):
super(Form1,self).__init__()
uic.loadUi('Form1.ui',self)
self.show()
self.newItem.clicked.connect(self.newItem_clicked)
def newItem_clicked(self):
self.form_second = Form2(self)
self.form_second.show()
#_________________MAIN____________________
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Form1()
sys.exit(app.exec_())
使用form1,form2,AllMethods三个类(AllMethods类中的form2调用方法并插入到form1中的列表中)
import sys
from PyQt4 import QtGui, uic
class AllMethods:
def __init__(self,parent):
self.parent = parent
def addListMethod(self,itm):
self.parent.itemList.addItem(itm)
#_________FORM2____________
class Form2(QtGui.QMainWindow):
def __init__(self,parent):
super(Form2,self).__init__(parent)
uic.loadUi('Form2.ui',self)
self.show()
self.parent = parent
self.insertItem.clicked.connect(self.insertItem_clicked)
def insertItem_clicked(self):
obj = AllMethods(self.parent)
obj.addListMethod(self.itemname.text())
#_________FORM1____________
class Form1(QtGui.QMainWindow):
def __init__(self):
super(Form1,self).__init__()
uic.loadUi('Form1.ui',self)
self.show()
self.newItem.clicked.connect(self.newItem_clicked)
def newItem_clicked(self):
self.form_second = Form2(self)
self.form_second.show()
#_________________MAIN____________________
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Form1()
sys.exit(app.exec_())