PDA

View Full Version : opening a second window



benlyboy
27th February 2010, 19:19
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,18 4).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.transla te("Dialog", "open window", None, QtGui.QApplication.UnicodeUTF8))



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,22 7).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,14 1,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.transla te("Dialog", "Close", None, QtGui.QApplication.UnicodeUTF8))



saved as "openwindow.py"




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.S IGNAL("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_())

prof.ebral
1st March 2010, 08:43
I think your code is pretty messy, to start with, making it harder to work with. Take a look at some of my code, because I am sure it will help you.

main.py:


# Version: Traipse 'Pious-Paladin'
# $Id: main.py,v Traipse 'Pious-Paladin' prof.ebral Exp $
#
# Description: This is the main entry point of the oprg application

from PyQt4 import QtCore, QtGui
from os import sep

from map_engine.map_dock import MapDock
from chat_engine.chat_dock import ChatDock
from net_engine.plist_dock import PListDock
from net_engine.client_gear import ClientGear
from tree_engine.tree_dock import TreeDock
from tool_gears.path_gear import PathTool, path

PathTool().load_paths(path)

class MainWindow(QtGui.QMainWindow):

def __init__(self):
super(MainWindow, self).__init__()
self.setWindowIcon(QtGui.QIcon(path['core_images'] + 'traipse-logo-2.png'))
self.client = ClientGear(self)
self.createActions()
self.createDocks()
self.StatusBar('Ready')
self.createToolBar()
self.createStyle()
self.setCentralWidget(self.center1)

def createDocks(self):
#Game Tree Docking Bay
self.treeDock = QtGui.QDockWidget("Dock-Game Tree", self)
self.center4 = TreeDock(self, self.client)
self.treeDock.setWidget(self.center4)
self.treeDock.setFeatures(QtGui.QDockWidget.DockWi dgetMovable |
QtGui.QDockWidget.DockWidgetFloatable)
self.center4.add('Child-2')
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.treeDock)

#Player List Docking Bay
self.PlayerList = QtGui.QDockWidget("Dock-Player List", self)
self.center3 = PListDock(self, self.client)
self.PlayerList.setWidget(self.center3)
self.PlayerList.setFeatures(QtGui.QDockWidget.Dock WidgetMovable |
QtGui.QDockWidget.DockWidgetFloatable)
self.center3.add(2, 'Player-2', 'Idle')
self.center3.add(3, 'Player-3', 'Typing')
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.PlayerList)

#Map Docking Bay
self.mapDock = QtGui.QDockWidget("Dock-Map", self)
self.center1 = MapDock(self, self.client)
self.mapDock.setWidget(self.center1)
self.mapDock.setFeatures(QtGui.QDockWidget.DockWid getMovable |
QtGui.QDockWidget.DockWidgetFloatable)
self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.mapDock)
self.mapDock.hide()


This is the Main file. This is my anchor to all my other UI scripts. This is not a complete version, but here you can see I am creating a proxy Player List and proxy Game Tree. I want you to look at the Map Docking Bay. It inserts the MapDock class, passing the parent and the future network client.

map_dock.py


from PyQt4 import QtCore, QtGui
from tool_gears.path_gear import path
from map_engine.tool_gear import mapToolGear
from map_engine.map_gear import MapDocket

class MapDock(QtGui.QMainWindow):

def __init__(self, parent, client):
super(MapDock, self).__init__()
self.parent = parent; self.client = client
self.createMap()
self.createToolBar()
self.setCentralWidget(self.mapDocket)

def createToolBar(self):
self.ToolBay = mapToolGear(self.parent)
self.addToolBar(QtCore.Qt.BottomToolBarArea, self.ToolBay)

## Map Docket
def createMap(self):
self.mapDocket = QtGui.QWidget()
layout = QtGui.QGridLayout()
self.mapView = MapDocket()
layout.addWidget(self.mapView)
self.mapDocket.setLayout(layout)

def createMapEditor(self):
self.parent.mapEditor = QtGui.QDialog()
layout = QtGui.QGridLayout()
self.parent.mapEditView = MapDocket()
layout.addWidget(self.parent.mapEditView)
self.parent.mapEditor.setLayout(layout)
self.parent.mapEditor.show()

def empty(self):
pass


Here is the entirety of my map_dock file, which contains the MapDock class. Can you see how I am re using the MapDocket() class to create a map for the core software and a map for the map editor? Maybe I am wrong, but I think that is what you are trying to do. Notice how the second one is rooted in the parent?

I would suggest you look at your Ui_Dialog Classes. They don't need to declared (object), the should be declared (QtGui.QDialog). I am also not sure why you would create a setupUI function and not use the pre-built __init__ function. I would suggest you use __init__ and then you can create room for you parent object, plus you can build the UI just by calling the class each time.

benlyboy
2nd March 2010, 03:41
thanks you have given me a lot to take in.

I only had a few minutes tonight so will look it over again tomorrow and post any questions or thoughts then.

thanks for the reply

prof.ebral
2nd March 2010, 04:08
You are welcome.