Replacing the existing MainWindow with a new window with Python, PyQt, Qt Designer

2024/11/15 20:47:54

I'm new to Python GUI programming I'm have trouble making a GUI app. I have a main window with only a button widget on it. What i want to know is how to replace the existing window with a new window when an event occurs (such as a button click).
An answer to a similar question here Replace CentralWidget in MainWindow, suggests using QStackedWidgets but they did not use Qt Designer to make their GUI apps whereas I have two .py files, one is the main window file and the other of window that i want to show after a button press take place, hence i don't know how to combine these two in my main.py file. For Example my main window looks like this:
Main Window
And after clicking on the button it should replace the existing window with this:
New Window
I would also like to know if the second window should be of type QStackedWindow, QDialog or QWidget?
Here is my main.py code

from PyQt4 import QtGui
import sys
import design, design1
import osclass ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):def __init__(self, parent=None):super(ExampleApp, self).__init__(parent)self.setupUi(self)self.btnBrowse.clicked.connect(self.doSomething)def doSomething(self):# Code to replace the main window with a new windowpassdef main():app = QtGui.QApplication(sys.argv)form = ExampleApp()form.show()app.exec_()if __name__ == '__main__':main()
Answer

You probably don't want to actually create and delete a bunch of windows, but if you really want to, you could do it like this

def doSomething(self):# Code to replace the main window with a new windowwindow = OtherWindow()window.show()self.close()

The in the OtherWindow class

class OtherWindow(...):...def doSomething(self):window = ExampleApp()window.show()self.close()

You actually probably don't want to do this. It would be much better if you simply created 1 main window, with a QStackedWidget and put the different controls and widgets on different tabs of the stacked widget and just switch between them in the same window.

https://en.xdnf.cn/q/71745.html

Related Q&A

Using LaTeX Beamer to display code

Im using the following LaTeX code in a Beamer presentation:\begin{frame}\begin{figure}\centering\tiny\lstset{language=python}\lstinputlisting{code/get_extent.py}\end{figure} \end{frame}Is it possible t…

Python metaclass and the object base class

After reading the excellent SO post, I tried crafting a module level metaclass:def metaclass(future_class_name, future_class_parents, future_class_attrs):print "module.__metaclass__"future_cl…

Is this the equivalent of a copy constructor in Python?

Im reviewing some old python code and came accross this pattern frequently:class Foo(object):def __init__(self, other = None):if other:self.__dict__ = dict(other.__dict__)Is this how a copy constructor…

Fabric error No handlers could be found for logger paramiko.transport

Im not sure why Im getting this error thats terminating my connection. I updated paramiko-1.7.6 from 1.7.5 via easy_install.Im trying to setup Fabric to upload my Django app to my server. The error see…

How to mix unbalanced Datasets to reach a desired distribution per label?

I am running my neural network on ubuntu 16.04, with 1 GPU (GTX 1070) and 4 CPUs.My dataset contains around 35,000 images, but the dataset is not balanced: class 0 has 90%, and class 1,2,3,4 share the …

Is there a way to prevent pandas to_json from adding \?

I am trying to send a pandas dataframe to_json and I am having some issues with the date. I am getting an addtional \ so that my records look like Updated:09\/06\/2016 03:09:44. Is it possible to not…

Convert SQL into json in Python [duplicate]

This question already has answers here:return SQL table as JSON in python(15 answers)Closed 8 years ago.I need to pass an object that I can convert using $.parseJSON. The query looks like this:cursor.e…

Django Middleware - How to edit the HTML of a Django Response object?

Im creating a custom middleware to django edit response object to act as a censor. I would like to find a way to do a kind of search and replace, replacing all instances of some word with one that I c…

Disable or restrict /o/applications (django rest framework, oauth2)

I am currently writing a REST API using Django rest framework, and oauth2 for authentication (using django-oauth-toolkit). Im very happy with both of them, making exactly what I want.However, I have on…

find command with exec in python subprocess gives error

Im trying to execute the following command using subprocess module (python)/usr/bin/find <filepath> -maxdepth 1 -type f -iname "<pattern>" -exec basename {} \;But, it gives the fo…