from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(833, 592)
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(5, 9, 1366, 700))
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("H:/Community/images/Library-Books2.jpg")))
self.label.setScaledContents(True)
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(40, 90, 91, 31))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Plantagenet Cherokee"))
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(160, 90, 361, 31))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(9)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(540, 92, 51, 31))
self.pushButton.setText(_fromUtf8(""))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("H:/Community/community/Library/build/classes/library/search.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon)
self.pushButton.setFlat(False)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.plainTextEdit = QtGui.QPlainTextEdit(Dialog)
self.plainTextEdit.setGeometry(QtCore.QRect(10, 140, 900, 391))
self.plainTextEdit.setBackgroundVisible(False)
self.plainTextEdit.setCenterOnScroll(False)
self.plainTextEdit.setObjectName(_fromUtf8("plainTextEdit"))
self.pushButton_2 = QtGui.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(410, 540, 90, 50))
self.pushButton_2.setText(_fromUtf8(""))
self.connect(self.pushButton, QtCore.SIGNAL('clicked()'), self.open)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("H:/Community/images/new-button3 - Copy.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_2.setIcon(icon1)
self.pushButton_2.setIconSize(QtCore.QSize(80, 80))
self.pushButton_2.setFlat(True)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def open(self):
fileName = QtGui.QFileDialog.getOpenFileName(self, 'OpenFile')
self.LineEdit.setText(fileName)
print(fileName)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label_2.setText(_translate("Dialog", "FileName", None))
Generally when I have windows working together I will set one as the others parent or have methods for one that receives data from the other. For example:
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
self.run_btn.clicked.connect(self.setup_project)
def setup_project(self):
self.project = ProjectParams(parent=self)
self.project.show()
if self.project.exec_() == QtWidgets.QDialog.Accepted:
self.params = self.project.params
self.loops = self.project.loops
class ProjectParams(QtWidgets.QDialog, Ui_ProjectParams):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent=parent)
self.setupUi(self)
self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False)
self.params = {}
From your error I would think you need to change:
self.connect(self.pushButton, QtCore.SIGNAL('clicked()'), self.open)
to:
self.pushButton.clicked.connect(self.open)
Related
Hello I am a student that is experimenting with GUI's and I was working in python making a simple log-in form and as I made the base layout I encountered an error where I could not solve the variables. This is the code:
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
def main():
app = QtGui.QApplication(sys.argv)
w = Ui_Dialog(__init__)
w.show()
sys.exit(app.exec_())
class Ui_Dialog(object):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.addWidgets()
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(380, 272)
Dialog.setMinimumSize(QtCore.QSize(380, 272))
Dialog.setMaximumSize(QtCore.QSize(380, 272))
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setCenterButtons(True)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.user_name = QtGui.QTextEdit(Dialog)
self.user_name.setGeometry(QtCore.QRect(110, 50, 221, 31))
self.user_name.setAutoFillBackground(False)
self.user_name.setObjectName(_fromUtf8("user_name"))
self.user_pass = QtGui.QTextEdit(Dialog)
self.user_pass.setGeometry(QtCore.QRect(110, 120, 221, 31))
self.user_pass.setAutoFillBackground(False)
self.user_pass.setObjectName(_fromUtf8("user_pass"))
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(20, 60, 81, 21))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(20, 120, 81, 21))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "USER NAME", None))
self.label_2.setText(_translate("Dialog", "PASSWORD", None))
if __name__ == "__main__":
main()
sorry if the alignment is off i am not used to having to use stack overflow but the code results in the following output:
Traceback (most recent call last):
File "C:\Users\todd7\Desktop\PY_auth\design.py", line 75, in <module>
main()
File "C:\Users\todd7\Desktop\PY_auth\design.py", line 28, in main
w = Ui_Dialog(__init__)
NameError: name '__init__' is not defined
and i am not sure how to fix the issue.
Try making your __init__ function in your Ui_Dialog class this:
def __init__(self, window, parent=None):
self.addWidgets()
self.setupUi(self)
window.show()
Then, in your main() function,
def main():
app = QtGui.QApplication(sys.argv)
w = Ui_Dialog(QtGui.QMainWindow())
sys.exit(app.exec_())
I am working with code http://pastebin.com/3JZWP14y to fix my errors in code below. I need to know the proper way to launch another widget/dialog for text entry to my list.
Below was my original code.
I have my app that displays a table, the child widget to add a row opens and closes fine but then my parent crashes. Code to parent is below. The child closes with sys.exit(app.exec_()) The child also opens the todo.p file but closes all pointers to file.
from PyQt4.QtGui import (QMainWindow, QApplication)
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import pickle as p
import sys
import addit
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def __init__(self):
#app = QApplication(sys.argv)
self.window = QMainWindow()
self.setupUi(self.window)
self.window.show()
#self.d=self.getdata()
self.d={"12345678":["do some stuff here that needs to be in a large cell to wrap text","1",1],"12343378":["do stuff","1",1]}
self.setmydata(self.d)
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(296, 478)
MainWindow.setIconSize(QtCore.QSize(0, 0))
MainWindow.setAnimated(False)
MainWindow.setTabShape(QtGui.QTabWidget.Rounded)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableView = QtGui.QTableWidget(self.centralwidget)
self.tableView.setGeometry(QtCore.QRect(20, 50, 261, 391))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableView.sizePolicy().hasHeightForWidth())
self.tableView.setSizePolicy(sizePolicy)
self.tableView.setEditTriggers(QtGui.QAbstractItemView.AllEditTriggers)
self.tableView.setDragEnabled(True)
self.tableView.setAlternatingRowColors(True)
self.tableView.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
self.tableView.setSortingEnabled(False)
self.tableView.setObjectName(_fromUtf8("tableView"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 10, 121, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(160, 10, 121, 31))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 296, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(lambda: self.openaddit())
def openaddit(self):
x=addit.openwindow()
print "clean"
#reload?
def createfile(self,d):
f=open('todo.d',"w")
p.dump(d,f)
f.close
def getdata(self):
if os.path.isfile('todo.p'):
print "here"
d= p.load(open('todo.p', 'r'))
else:
print "here2"
d={}
self.createfile(d)
print d,"hereeeee"
return d
def setmydata(self,d):
self.data = {}
keys=d.keys()
self.data['col1']=keys
self.data['col2']=[]
self.tableView.setRowCount(len(keys))
self.tableView.setColumnCount(2)
self.tableView.setHorizontalHeaderLabels(['SR', 'Notes'])
for i in range(0,len(keys)):
if d[keys[i]][2]==0:
continue
self.tableView.setItem(i, 0, QtGui.QTableWidgetItem(keys[i]))
self.tableView.setItem(i, 1, QtGui.QTableWidgetItem(d[keys[i]][0]))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.tableView.setRowHeight(i,100)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Add New", None))
self.pushButton_2.setText(_translate("MainWindow", "Save", None))
app = QApplication(sys.argv)
frame = Ui_MainWindow()
#frame.show()
app.exec_()
addit.openwindow() calls the below program.
import sys
from PyQt4 import QtCore, QtGui
#import numpy as np
import pickle as p
class MyQDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(MyQDialog, self).__init__(parent)
## Default values
self.d={}
## Create labels and buttons
#self.setObjectName(("Dialog"))
self.resize(400, 279)
self.d ={}
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(("buttonBox"))
self.lineEdit = QtGui.QLineEdit(self)
self.lineEdit.setGeometry(QtCore.QRect(40, 20, 141, 31))
self.lineEdit.setObjectName(("lineEdit"))
self.textEdit = QtGui.QTextEdit(self)
self.textEdit.setGeometry(QtCore.QRect(20, 60, 361, 161))
self.textEdit.setObjectName(("textEdit"))
self.textEdit.setTabChangesFocus(True)
self.spinBox = QtGui.QSpinBox(self)
self.spinBox.setGeometry(QtCore.QRect(260, 21, 61, 31))
self.spinBox.setMinimum(1)
self.spinBox.setObjectName(("spinBox"))
self.label = QtGui.QLabel(self)
self.label.setGeometry(QtCore.QRect(10, 26, 21, 21))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(("label"))
self.label.setText("SR")
self.label_2 = QtGui.QLabel(self)
self.label_2.setGeometry(QtCore.QRect(190, 25, 61, 21))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(("label_2"))
self.label_2.setText("Priority")
self.setTabOrder(self.lineEdit,self.spinBox)
self.setTabOrder(self.spinBox,self.textEdit)
self.lineEdit.setFocus(True)
self.setWindowTitle("Add To Do SR")
self.show()
## Button functions
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(("accepted()")), self.build)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(("accepted()")), self.close)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(("rejected()")), self.cancel)
def cancel(self):
self.close()
def save(self):
#self.close()
return self.d
def build(self):
#if(len(self.lineEdit.text()!=0)
#print len(self.lineEdit.text())
self.d["SR"]=str(unicode(self.lineEdit.text().toUtf8(), encoding="UTF-8"))
self.d["desc"]=str(unicode(self.textEdit.toPlainText().toUtf8(), encoding="UTF-8"))
self.d["p"]=str(self.spinBox.value())
#print self.d
if self.d["SR"]!='':
#print "saving"
f= open('todo.p', 'r+')
load=p.load(f)
f.close()
#print load
tmp2=[]
tmp2.append(self.d["desc"])
tmp2.append(self.d["p"])
tmp2.append(1)
load[self.d['SR']]=tmp2
#print tmp
#print load
f=open('todo.p','w')
p.dump(load,f)
f.close()
print "closed"
self.save
else:
self.d={}
return False
def openwindow():
app = QtGui.QApplication(sys.argv)
edit_params__QD = MyQDialog()
edit_params__QD.exec_()
edit_params__QD.activateWindow()
if edit_params__QD.result() == 0:
d= edit_params__QD.save()
if len(d)==0:
#print "did not add no SR num"
return False
print "exited out"
#sys.exit(app.exex_())
I want to open new window from existing one in pyqt.
My source code has two functions.
one functions : when button is clicked, File Dialog is opened.
another functions : when another button is clicked, New Window is
opened.
But When I want to New window, error message occurs :
Traceback (most recent call last): File "C:\Users\Han\Desktop\project\prototype\newwindow.py", line 61, in check_start self.w.show() AttributeError: 'Ui_dialog' object has no attribute 'show'
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(518, 349)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(40, 40, 141, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(400, 330, 141, 16))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayoutWidget = QtGui.QWidget(Form)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(70, 200, 381, 31)) self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.textEdit = QtGui.QTextEdit(self.horizontalLayoutWidget)
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.horizontalLayout.addWidget(self.textEdit)
self.pushButton = QtGui.QPushButton(self.horizontalLayoutWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(230, 270, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.file_open)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.check_start)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Off_Strip", None))
self.label.setText(_translate("Form", "123", None))
self.label_2.setText(_translate("Form", "#Copyright MokLab", None))
self.pushButton.setText(_translate("Form", "123", None))
self.pushButton_2.setText(_translate("Form", "123!", None))
def file_open(self):
fname = QtGui.QFileDialog.getOpenFileName()
f = open(fname, 'r')
f.close()
def check_start(self):
self.w = Ui_dialog()
self.w.show()
class Ui_dialog(object):
def setupUi(self, dialog):
dialog.setObjectName(_fromUtf8("dialog"))
dialog.resize(400, 300)
self.pushButton = QtGui.QPushButton(dialog)
self.pushButton.setGeometry(QtCore.QRect(190, 130, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.radioButton = QtGui.QRadioButton(dialog)
self.radioButton.setGeometry(QtCore.QRect(30, 70, 90, 16))
self.radioButton.setObjectName(_fromUtf8("radioButton"))
self.radioButton_2 = QtGui.QRadioButton(dialog)
self.radioButton_2.setGeometry(QtCore.QRect(40, 110, 90, 16))
self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
self.retranslateUi(dialog)
QtCore.QMetaObject.connectSlotsByName(dialog)
def retranslateUi(self, dialog):
dialog.setWindowTitle(_translate("dialog", "Form", None))
self.pushButton.setText(_translate("dialog", "PushButton", None))
self.radioButton.setText(_translate("dialog", "RadioButton", None))
self.radioButton_2.setText(_translate("dialog", "RadioButton", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
The line self.w.show, where you've declared w as a Ui_dialog object, will attempt to call the show instance variable inside Ui_dialog, which doesn't work because you've never defined any function called show.
It doesn't help that you've inherited object for class Ui_dialog. You should probably inherit QtGui.QDialog for that class, and rewrite your class accordingly.
I want to open new window from existing one in pyqt.
My source code has two functions.
one functions : when button is clicked, File Dialog is opened.
another functions : when another button is clicked, New Window is
opened.
But When I want to New window, error message occurs :
Traceback (most recent call last): File "C:\Users\Han\Desktop\project\prototype\newwindow.py", line 61, in check_start self.w.show() AttributeError: 'Ui_dialog' object has no attribute 'show'
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(518, 349)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(40, 40, 141, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(400, 330, 141, 16))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayoutWidget = QtGui.QWidget(Form)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(70, 200, 381, 31)) self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.textEdit = QtGui.QTextEdit(self.horizontalLayoutWidget)
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.horizontalLayout.addWidget(self.textEdit)
self.pushButton = QtGui.QPushButton(self.horizontalLayoutWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(230, 270, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.file_open)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.check_start)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Off_Strip", None))
self.label.setText(_translate("Form", "123", None))
self.label_2.setText(_translate("Form", "#Copyright MokLab", None))
self.pushButton.setText(_translate("Form", "123", None))
self.pushButton_2.setText(_translate("Form", "123!", None))
def file_open(self):
fname = QtGui.QFileDialog.getOpenFileName()
f = open(fname, 'r')
f.close()
def check_start(self):
self.w = Ui_dialog()
self.w.show()
class Ui_dialog(object):
def setupUi(self, dialog):
dialog.setObjectName(_fromUtf8("dialog"))
dialog.resize(400, 300)
self.pushButton = QtGui.QPushButton(dialog)
self.pushButton.setGeometry(QtCore.QRect(190, 130, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.radioButton = QtGui.QRadioButton(dialog)
self.radioButton.setGeometry(QtCore.QRect(30, 70, 90, 16))
self.radioButton.setObjectName(_fromUtf8("radioButton"))
self.radioButton_2 = QtGui.QRadioButton(dialog)
self.radioButton_2.setGeometry(QtCore.QRect(40, 110, 90, 16))
self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
self.retranslateUi(dialog)
QtCore.QMetaObject.connectSlotsByName(dialog)
def retranslateUi(self, dialog):
dialog.setWindowTitle(_translate("dialog", "Form", None))
self.pushButton.setText(_translate("dialog", "PushButton", None))
self.radioButton.setText(_translate("dialog", "RadioButton", None))
self.radioButton_2.setText(_translate("dialog", "RadioButton", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
The line self.w.show, where you've declared w as a Ui_dialog object, will attempt to call the show instance variable inside Ui_dialog, which doesn't work because you've never defined any function called show.
It doesn't help that you've inherited object for class Ui_dialog. You should probably inherit QtGui.QDialog for that class, and rewrite your class accordingly.
I am new to PYQT and I am trying to get a feel for it. I am trying to populate a table widget with 5 different push buttons. I need to clear out the table between each button push, but would like to leave the headers I have. I have tried a tablewidget clearcontents. but that clears everything and I am unsure of where to place it properly so it will clear the contents before each button push, that brings in the new info.
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(761, 637)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(0, 550, 201, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.get_output1_statement)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(0, 580, 201, 31))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton.clicked.connect(self.get_output2_statement)
self.pushButton_3 = QtGui.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(0, 610, 201, 31))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.pushButton_3.clicked.connect(self.get_output3_statement)
self.pushButton_4 = QtGui.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(250, 580, 201, 31))
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.pushButton.clicked.connect(self.get_output4_statement)
self.pushButton_5 = QtGui.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(510, 580, 201, 31))
self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
self.pushButton.clicked.connect(self.get_output5_statement)
self.tableWidget = QtGui.QTableWidget(Form)
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 741, 551))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(5)
sizePolicy.setVerticalStretch(5)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(50)
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("name"))
self.tableWidget.setItem(0,1,QtGui.QTableWidgetItem("column_1"))
self.tableWidget.setItem(0,2,QtGui.QTableWidgetItem("column_2"))
self.tableWidget.setItem(0,3,QtGui.QTableWidgetItem("column_3"))
self.tableWidget.setItem(0,4,QtGui.QTableWidgetItem("column_4"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "button1", None))
self.pushButton_2.setText(_translate("Form", "button2", None))
self.pushButton_3.setText(_translate("Form", "button3", None))
self.pushButton_4.setText(_translate("Form", "button4", None))
self.pushButton_5.setText(_translate("Form", "button5", None))
You can use the clearContents() method when you set the table headers in an appropriate way.
change:
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("name"))
self.tableWidget.setItem(0,1,QtGui.QTableWidgetItem("column_1"))
self.tableWidget.setItem(0,2,QtGui.QTableWidgetItem("column_2"))
self.tableWidget.setItem(0,3,QtGui.QTableWidgetItem("column_3"))
self.tableWidget.setItem(0,4,QtGui.QTableWidgetItem("column_4"))
to:
self.tableWidget.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("name"))
self.tableWidget.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("column_1"))
self.tableWidget.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("column_2"))
self.tableWidget.setHorizontalHeaderItem(3, QtGui.QTableWidgetItem("column_3"))
self.tableWidget.setHorizontalHeaderItem(4, QtGui.QTableWidgetItem("column_4"))
EDIT Extra code after comments
Maybe this helps you? I tried keeping as much of your code.
class Ui_Form(QtGui.QDialog):
def __init__(self, parent=None):
super(Ui_Form, self).__init__(parent)
self.setObjectName(_fromUtf8("Form"))
self.resize(761, 637)
self.pushButton = QtGui.QPushButton()
self.pushButton.setGeometry(QtCore.QRect(0, 550, 201, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.connect(self.pushButton, QtCore.SIGNAL("released()"), self.get_output1_statement)
self.tableWidget = QtGui.QTableWidget()
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 741, 551))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(5)
sizePolicy.setVerticalStretch(5)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(50)
self.tableWidget.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("name"))
layout = QtGui.QVBoxLayout()
layout.addWidget(self.tableWidget)
layout.addWidget(self.pushButton)
self.setLayout(layout)
for i in range(1, 5):
self.tableWidget.setHorizontalHeaderItem(i, QtGui.QTableWidgetItem("column_{0}".format(i)))
self.retranslateUi()
def retranslateUi(self):
self.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "button1", None))
def get_output1_statement(self):
self.tableWidget.clearContents()
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("add some data"))
app = QtGui.QApplication(sys.argv)
form = Ui_Form()
form.show()
app.exec_()