How to show all UI from another python file in current file when button is clicked.
-
So you know how to import a class from another file. The same applies to the widgets you implement in a different file.
from PySide2.QtWidgets import QWidget class MyWidget(QWidget): """My Cool Widget""" def doSomething(self): """Do something cool""" print("Done something cool")
import sys from PySide2.QtWidgets import QApplication from .mywidget import MyWidget if __name__ == "__main__": app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_())
-
What exactly are you trying to achieve ? It looks as if you want to use something you know nothing about so please explain your use case.
-
What exactly are you trying to achieve ? It looks as if you want to use something you know nothing about so please explain your use case.
Hi
I have three different types of widgets in a python file in the form of classes and functions and I want to know how I can import them all at once to another python file so that I can show them in the python file. I need to use pyqt5. Please give me examples. The types of widgets you use does not matter to me.
Thanks
-
Hi
Did you try something like
from .name_of_file import *Also, did you see
https://www.riverbankcomputing.com/static/Docs/PyQt5/ -
Hi
Did you try something like
from .name_of_file import *Also, did you see
https://www.riverbankcomputing.com/static/Docs/PyQt5/ -
Thanks I understand that now. But how would I show all the widgets once I have used the import statement.
Thanks
@sdf1444
Hi
There is no magic to it.
The import merely makes the types (classes) known.
You must create instances of the classes you wish to usewidget = MyWidget()
MyWidget will of cause be another name, depending on what you import.
-
@sdf1444
Hi
There is no magic to it.
The import merely makes the types (classes) known.
You must create instances of the classes you wish to usewidget = MyWidget()
MyWidget will of cause be another name, depending on what you import.
-
What happens if I want to import the classes and functions but I don't know what the classes and functions are called.
Thanks
@sdf1444
Then you open the py file and look what they are called ?
In any case, you need to know the classnames or function names to use them.
However, i think you can enumerate them as to list their names or similar in a list
https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python
However, concrete names must be used to actually use them. -
@sdf1444
Then you open the py file and look what they are called ?
In any case, you need to know the classnames or function names to use them.
However, i think you can enumerate them as to list their names or similar in a list
https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python
However, concrete names must be used to actually use them. -
So I must no the class names if I want to import them? There is no way to import and show all the classes without having to know what they are called?
Thanks
@sdf1444
well you can use the * syntax to import all, but
you must create instances to show them so you cant say
"Import all and show them " as that is not how it works.
Also if the module contains multiple widgets. Then the only option it would
have was to show all widgets as separate windows as there would be no
way to know in what way to combine them into a single window.
However, the module could internally have multiple widgets defined and
and have one widget that uses the tother so you would only need to create
an instance of that one.Can you explain why you seem to hook on not using the class names?
-
@sdf1444
well you can use the * syntax to import all, but
you must create instances to show them so you cant say
"Import all and show them " as that is not how it works.
Also if the module contains multiple widgets. Then the only option it would
have was to show all widgets as separate windows as there would be no
way to know in what way to combine them into a single window.
However, the module could internally have multiple widgets defined and
and have one widget that uses the tother so you would only need to create
an instance of that one.Can you explain why you seem to hook on not using the class names?
Hi
I want to be able to import and show widgets from any other file in current python file but the condition is I have no idea what that file contains but I should be able to still show the widgets from that file. Please show me how to do this. As simple as possible would be great.
Thanks
-
Hi
It seems you can use a string as class name and in that way maybe do it
with
https://stackoverflow.com/questions/553784/can-you-use-a-string-to-instantiate-a-class
combined with
https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python
so you import module , build stringlist and
then use thedef construct(self, builderName): targetClass = getattr(idClasses, builderName) instance = targetClass() self.allClasses.append(instance)
to actually construct a list of instances.
Then you can insert into your main window to show.
-
Hi
It seems you can use a string as class name and in that way maybe do it
with
https://stackoverflow.com/questions/553784/can-you-use-a-string-to-instantiate-a-class
combined with
https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python
so you import module , build stringlist and
then use thedef construct(self, builderName): targetClass = getattr(idClasses, builderName) instance = targetClass() self.allClasses.append(instance)
to actually construct a list of instances.
Then you can insert into your main window to show.
-
@sdf1444 you do realise that you are basically asking us to do your work for you ? You were already given several good starting points but you didn't even show anything you tried to get things working.
Hi
Here is some code to display widgets in pyqt5. But how do I allow the code so that it displays the imported widget from another file in the same python file/window without creating a new window for the new widget.
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from pyqt2 import * class App(QWidget): def __init__(self): QMainWindow.__init__(self) pybutton = QPushButton('Click me', self) pybutton.resize(200,70) pybutton.move(400, 50) self.show() if __name__ == "__main__": app = QApplication(sys.argv) mainWin = App() mainWin2 = App2() sys.exit(app.exec_())
Here is also the code for the imported python file:
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * class App2(QWidget): def __init__(self): QMainWindow.__init__(self) pybutton = QPushButton('hi', self) pybutton.resize(200,70) pybutton.move(900, 50) self.show()
-
Hi
It seems you can use a string as class name and in that way maybe do it
with
https://stackoverflow.com/questions/553784/can-you-use-a-string-to-instantiate-a-class
combined with
https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python
so you import module , build stringlist and
then use thedef construct(self, builderName): targetClass = getattr(idClasses, builderName) instance = targetClass() self.allClasses.append(instance)
to actually construct a list of instances.
Then you can insert into your main window to show.
Hi
Here is some code to display widgets in pyqt5. But how do I allow the code so that it displays the imported widget from another file in the same python file/window without creating a new window for the new widget.
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from pyqt2 import * class App(QWidget): def init(self): QMainWindow.init(self) pybutton = QPushButton('Click me', self) pybutton.resize(200,70) pybutton.move(400, 50) self.show() if name == "main": app = QApplication(sys.argv) mainWin = App() mainWin2 = App2() sys.exit(app.exec_())
Here is also the code for the imported python file:
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * class App2(QWidget): def init(self): QMainWindow.init(self) pybutton = QPushButton('hi', self) pybutton.resize(200,70) pybutton.move(900, 50) self.show()
-
Hi
Widgets that do not get a parent assigned, will become windows.
So normally you would insert the imported widgets into a
layout on the mainwindow. -
Hi
Widgets that do not get a parent assigned, will become windows.
So normally you would insert the imported widgets into a
layout on the mainwindow. -
Hi
How do I do this? What is the code for this because I really don't understand how to do it.
Thanks very much much appreciated with also your help.
@sdf1444
well something like
layout = QHBoxLayout()
layout.addWidget(button1) // button1 would be your imported widget instancewindow.setLayout(layout)
window.show()window most like being mainwindow
its a good idea to read about layouts
https://doc.qt.io/qtforpython/overviews/layout.html
so you know what you are using :) -
@sdf1444
well something like
layout = QHBoxLayout()
layout.addWidget(button1) // button1 would be your imported widget instancewindow.setLayout(layout)
window.show()window most like being mainwindow
its a good idea to read about layouts
https://doc.qt.io/qtforpython/overviews/layout.html
so you know what you are using :)