Hi all i'm new to python and qt
I have played a little with c++ and basic ,But most of the programing I have done in the past was for the web using java script, so this is all new to me.
I am working on a program, and would like to open a window when I push on a button. so fare every thing I have tried has failed. There really doesn't seem to be a good tutorial on this, so I was hoping some would post a simple example of how this is done.
To help with this I have posted three files two are the window files the last one is starting point for the program.
I think other newbies will find this interesting
Thanks for any help
Saved as "openwindowmain.py"
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'openwindowmain.ui'
#
# Created: Sun Feb 21 18:59:44 2010
# by: PyQt4 UI code generator 4.3.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.
resize(QtCore.
QSize(QtCore.
QRect(0,
0,
268,
184).
size()).
expandedTo(Dialog.
minimumSizeHint()))
self.
pushButton.
setGeometry(QtCore.
QRect(60,
60,
161,
61)) self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
def retranslateUi(self, Dialog):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'openwindowmain.ui'
#
# Created: Sun Feb 21 18:59:44 2010
# by: PyQt4 UI code generator 4.3.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,268,184).size()).expandedTo(Dialog.minimumSizeHint()))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(60,60,161,61))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "open window", None, QtGui.QApplication.UnicodeUTF8))
To copy to clipboard, switch view to plain text mode
Saved as "openwindowchild.py"
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'openwindowchild.ui'
#
# Created: Sat Feb 27 13:49:23 2010
# by: PyQt4 UI code generator 4.3.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.
resize(QtCore.
QSize(QtCore.
QRect(0,
0,
203,
227).
size()).
expandedTo(Dialog.
minimumSizeHint()))
self.
label = QtGui.
QLabel(Dialog
) self.
label.
setGeometry(QtCore.
QRect(50,
70,
131,
51)) self.label.setObjectName("label")
self.
pushButton.
setGeometry(QtCore.
QRect(40,
160,
141,
51)) self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
def retranslateUi(self, Dialog):
self.
label.
setText(QtGui.
QApplication.
translate("Dialog",
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Sans Serif\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:25pt; color:#ff7f50;\">Worked</span></p></body></html>", None, QtGui.
QApplication.
UnicodeUTF8))
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'openwindowchild.ui'
#
# Created: Sat Feb 27 13:49:23 2010
# by: PyQt4 UI code generator 4.3.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,203,227).size()).expandedTo(Dialog.minimumSizeHint()))
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(50,70,131,51))
self.label.setObjectName("label")
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(40,160,141,51))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Dialog", "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Sans Serif\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:25pt; color:#ff7f50;\">Worked</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Close", None, QtGui.QApplication.UnicodeUTF8))
To copy to clipboard, switch view to plain text mode
saved as "openwindow.py"
import sys
from PyQt4 import QtCore, QtGui
from openwindowmain import Ui_Dialog
def __init__(self, parent=None):
QtGui.
QWidget.__init__
(self, parent
) self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.
QObject.
connect(self.
ui.
pushButton,QtCore.
SIGNAL("clicked()"), self.
text_handler)
def text_handler(self):
print "worked"
if __name__ == "__main__":
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
import sys
from PyQt4 import QtCore, QtGui
from openwindowmain import Ui_Dialog
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.pushButton,QtCore.SIGNAL("clicked()"), self.text_handler)
def text_handler(self):
print "worked"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks