PDA

View Full Version : How to open a 2nd form after a button is clicked in the 1st - PyQT



hyoumoku
1st February 2011, 07:35
I have read this thread (http://www.qtcentre.org/threads/27297-Opening-another-created-gui-pyqt) and also search and found the FAQ here. (http://www.qtcentre.org/faq.php?faq=qt_general_category#faq_qt_designer_2f orms)

But the problem with the first thread mentioned above is that it was written using one source file. I've been trying to google for hours now and search for a solution to connect two forms I made using eric4 + QtDesigner. Most of the tutorials I found uses only one code like that of the first thread mentioned above.

The FAQ that was pointed out is in C++ I believe but I can't seem to deduce it to code it up for PyQt

Is it by chance possible the dialogue code made by the first form will then connect to the second form in PyQt (in eric4)? I generated the Dialog code like it was mentioned here at the eric4 web browser tutorial (http://eric-ide.python-projects.org/tutorials/MiniBrowser/chapter3.html).

After I made a new Dialog form, I want the Main Window form to connect to the Dialog form I made but was stuck now how to do it. Would it be alright by chance to ask how to code it?

My apologies as I'm quite new to this and thank you in advance *bows deeply*

hyoumoku
1st February 2011, 11:49
Okay, I think I kinda got it - I imported the class of the second form. This is somewhat the codes (but I changed the names of the classes mostly)

form1.py

from PyQt4.QtGui import QMainWindow
from PyQt4.QtCore import pyqtSignature

from Ui_form1 import Ui_MainWindow

from ui.form2 import FormTwo

class FormOne(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
def __init__(self, parent = None):
"""
Constructor
"""
QMainWindow.__init__(self, parent)
self.setupUi(self)

@pyqtSignature("")
def on_button_released(self):
"""
Slot documentation goes here.
"""
FT = FormTwo()
FT.show()

form2.py

from PyQt4.QtGui import QDialog
from PyQt4.QtCore import pyqtSignature

from Ui_form2 import Ui_Dialog

class FormTwo(QDialog, Ui_Dialog):
"""
Class documentation goes here.
"""
def __init__(self, parent = None):
"""
Constructor
"""
QDialog.__init__(self, parent)
self.setupUi(self)

__init__.py

from PyQt4 import QtCore, QtGui
from ui.form1 import FormOne

if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = FormOne()
ui.show()
sys.exit(app.exec_())

There is a new problem though that the 2nd form doesn't stay at all and closes immediately after I click the button on the first form. Would it be alright to ask how to correct and solve this?

hyoumoku
3rd February 2011, 07:25
Finally got it. It should be:

self.FT = FormTwo()
self.FT.show()

so that the next form would appear. Thanks anyway guys :) Posting just in case for others if they see this :P

PS: Now that I realize it, that was embarrassing OTL;;

thatanswerguy
11th June 2012, 15:21
once the second form is created, make a button for the new form. this code is actually one of the easiest. if you want to hide the current window. (for this example I will just use the genaric form2)

me.hide()
form2.Show()