In PyQt4 how do i initiate a window - python

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

Related

how to connect two windows in pyqt4?

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)

AttributeError: 'dataAcquisition' object has no attribute 'gpioButton'

I'm trying to connect a push-button on my main window to open a new widget when I click it. But each time I run the program I get this error:
AttributeError: 'dataAcquisition' object has no attribute 'gpioButton'
I will be grateful if someone can help.
Here's my code:
from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
from gpiodialog import Ui_GPIODialog
import sys
class gpioDialog(QtGui.QDialog, Ui_GPIODialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
self.setWidowFlags(flags)
self.ui = Ui_GPIODialog
self.ui.setupUi(self)
self.gpioOKButton.clicked.connect(self.acceptOKButtonClicked)
def acceptOKButtonClicked(self):
self.close()
class dataAcquisition(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.gpioButton.clicked.connect(self.gpioButton_clicked)
self.popGPIO = gpioDialog()
def gpioButton_clicked(self):
self.popGPIO.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
MainWindow = dataAcquisition()
MainWindow.show()
sys.exit(app.exec_())
mainwindow.py code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Tue Oct 18 02:57:57 2016
# by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(700, 600)
MainWindow.setMinimumSize(QtCore.QSize(700, 600))
MainWindow.setMaximumSize(QtCore.QSize(700, 600))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gpioButton = QtGui.QToolButton(self.centralwidget)
self.gpioButton.setGeometry(QtCore.QRect(20, 80, 181, 61))
self.gpioButton.setObjectName(_fromUtf8("gpioButton"))
self.procButton = QtGui.QPushButton(self.centralwidget)
self.procButton.setGeometry(QtCore.QRect(20, 180, 181, 61))
self.procButton.setObjectName(_fromUtf8("procButton"))
self.startButton = QtGui.QPushButton(self.centralwidget)
self.startButton.setEnabled(True)
self.startButton.setGeometry(QtCore.QRect(20, 360, 131, 51))
self.startButton.setObjectName(_fromUtf8("startButton"))
self.stopButton = QtGui.QPushButton(self.centralwidget)
self.stopButton.setGeometry(QtCore.QRect(190, 360, 131, 51))
self.stopButton.setObjectName(_fromUtf8("stopButton"))
self.plotButton = QtGui.QPushButton(self.centralwidget)
self.plotButton.setGeometry(QtCore.QRect(20, 460, 181, 61))
self.plotButton.setObjectName(_fromUtf8("plotButton"))
self.columnView = QtGui.QColumnView(self.centralwidget)
self.columnView.setGeometry(QtCore.QRect(340, 50, 341, 251))
self.columnView.setObjectName(_fromUtf8("columnView"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(280, 20, 221, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.saveButton = QtGui.QPushButton(self.centralwidget)
self.saveButton.setEnabled(True)
self.saveButton.setGeometry(QtCore.QRect(410, 420, 211, 31))
self.saveButton.setObjectName(_fromUtf8("saveButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 700, 27))
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)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.gpioButton.setText(_translate("MainWindow", "GPIO Number of inputs", None))
self.procButton.setText(_translate("MainWindow", "Processing", None))
self.startButton.setText(_translate("MainWindow", "Start Acquiring", None))
self.stopButton.setText(_translate("MainWindow", "Stop Acquiring", None))
self.plotButton.setText(_translate("MainWindow", "Start Live Plot", None))
self.label_2.setText(_translate("MainWindow", "Data Acquisition System", None))
self.saveButton.setText(_translate("MainWindow", "Save this session\'s data", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
gpiodialog.py code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gpiodialog.ui'
#
# Created: Tue Oct 18 03:11:51 2016
# by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!
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_GPIODialog(object):
def setupUi(self, GPIODialog):
GPIODialog.setObjectName(_fromUtf8("GPIODialog"))
GPIODialog.resize(400, 300)
self.gpioOKButton = QtGui.QPushButton(GPIODialog)
self.gpioOKButton.setGeometry(QtCore.QRect(170, 240, 101, 31))
self.gpioOKButton.setObjectName(_fromUtf8("gpioOKButton"))
self.label = QtGui.QLabel(GPIODialog)
self.label.setGeometry(QtCore.QRect(90, 20, 191, 21))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(GPIODialog)
QtCore.QMetaObject.connectSlotsByName(GPIODialog)
def retranslateUi(self, GPIODialog):
GPIODialog.setWindowTitle(_translate("GPIODialog", "Dialog", None))
self.gpioOKButton.setText(_translate("GPIODialog", "OK", None))
self.label.setText(_translate("GPIODialog", "Choose GPIO input bins", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
GPIODialog = QtGui.QDialog()
ui = Ui_GPIODialog()
ui.setupUi(GPIODialog)
GPIODialog.show()
sys.exit(app.exec_())
You created a separate namespace for the ui, so you would need to access the button like this:
self.ui.gpioButton.clicked.connect(self.gpioButton_clicked)
However, if you'd prefer to have all the widgets as attributes of the window, do this:
class gpioDialog(QtGui.QDialog, Ui_GPIODialog):
def __init__(self, parent=None):
super(gpioDialog, self).__init__(parent)
flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
self.setWindowFlags(flags)
self.setupUi(self)
self.gpioOKButton.clicked.connect(self.acceptOKButtonClicked)
class dataAcquisition(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(dataAcquisition, self).__init__(parent)
self.setupUi(self)
self.gpioButton.clicked.connect(self.gpioButton_clicked)
self.popGPIO = gpioDialog()

In pyqt4, How do i open new window from existing one?

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.

open new window in pyqt [duplicate]

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.

Opening a window from another window in python using pyqt framework

I am trying open a pyqt window from another pyqt window on clicking a button but i can't really get a hold do it . Both the python files opening.py and signup.py can run standalone on their own but i can't think of a way to link them ...(Running signup.py from opening.py after clicking signup button)
opening.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'opening.ui'
#
# Created: Tue Jan 20 00:19:45 2015
# by: PyQt4 UI code generator 4.10.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
#from signup import Ui_Dialog1
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(290, 237)
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(70, 19, 151, 41))
self.label.setObjectName(_fromUtf8("label"))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(40, 170, 201, 41))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(40, 100, 201, 41))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton.clicked.connect(self.handleTest1)
self.pushButton_2.clicked.connect(self.handleTest2)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "MAIL SERVER", None))
self.pushButton.setText(_translate("Dialog", "SIGN UP", None))
self.pushButton_2.setText(_translate("Dialog", "LOGIN", None))
def handleTest1(self):
#self.accept()
#self.h=Ui_Dialog1()
#self.h.setupUi(QtGui.QDialog())
pass
def handleTest2(self):
execfile('login.py')
import sys
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
and other file which is to be opened after clicking signup button (login button doesn't work as of now)
signup.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'abc.ui'
#
# Created: Mon Jan 19 23:35:37 2015
# by: PyQt4 UI code generator 4.10.4
#
# WARNING! All changes made in this file will be lost!
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_Dialog1(object):
def setupUi(self, Dialog):
print "16"
Dialog.setObjectName(_fromUtf8("SignUp"))
Dialog.resize(415, 364)
self.SignUp = QtGui.QDialogButtonBox(Dialog)
self.SignUp.setGeometry(QtCore.QRect(130, 310, 176, 27))
self.SignUp.setOrientation(QtCore.Qt.Horizontal)
self.SignUp.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.SignUp.setObjectName(_fromUtf8("SignUp"))
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(50, 50, 91, 21))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(50, 110, 66, 17))
self.label_2.setText(_fromUtf8(""))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(50, 100, 66, 17))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.label_4 = QtGui.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(50, 150, 66, 17))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.label_5 = QtGui.QLabel(Dialog)
self.label_5.setGeometry(QtCore.QRect(50, 200, 121, 17))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.label_6 = QtGui.QLabel(Dialog)
self.label_6.setGeometry(QtCore.QRect(50, 250, 91, 17))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.lineEdit_3 = QtGui.QLineEdit(Dialog)
self.lineEdit_3.setGeometry(QtCore.QRect(200, 140, 181, 27))
self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
self.lineEdit_4 = QtGui.QLineEdit(Dialog)
self.lineEdit_4.setGeometry(QtCore.QRect(200, 190, 181, 27))
self.lineEdit_4.setObjectName(_fromUtf8("lineEdit_4"))
self.lineEdit_5 = QtGui.QLineEdit(Dialog)
self.lineEdit_5.setGeometry(QtCore.QRect(200, 90, 181, 27))
self.lineEdit_5.setObjectName(_fromUtf8("lineEdit_5"))
self.lineEdit_6 = QtGui.QLineEdit(Dialog)
self.lineEdit_6.setGeometry(QtCore.QRect(200, 40, 181, 27))
self.lineEdit_6.setObjectName(_fromUtf8("lineEdit_6"))
self.lineEdit_7 = QtGui.QLineEdit(Dialog)
self.lineEdit_7.setGeometry(QtCore.QRect(200, 240, 181, 27))
self.lineEdit_7.setObjectName(_fromUtf8("lineEdit_7"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.SignUp, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.SignUp, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
print "17"
Dialog.show()
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("SignUp", "SignUp", None))
self.label.setText(_translate("Dialog", "Name", None))
self.label_3.setText(_translate("Dialog", "E-mail", None))
self.label_4.setText(_translate("Dialog", "Password", None))
self.label_5.setText(_translate("Dialog", "Confirm Password", None))
self.label_6.setText(_translate("Dialog", "Mobile (+91)", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog1()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
The commented part in opening.py is the one creating problems . Help would be appreciated .
Your Ui_Dialog1 should inherit from QDialog, which makes it an UI object, but you inherit from object, this has no "show" method.
def handleTest1(self):
#self.accept()
#self.h=Ui_Dialog1() # You need to move this to __init__ function
#self.h.setupUi(QtGui.QDialog()) # You need to move this to __init__ function
self.h.show() # call the show method here
You need to move the self.h = Ui_Dialog1() and setupUi to init functions, this will avoid possible memory leak. (otherwise each time you click "sigunup", it will create a new object)

Categories

Resources