How to use Python to implement multiple windows switching for a plugin.

How to use Python to implement multiple windows switching for a plugin.

zhujianchen
Contributor Contributor
768 Views
7 Replies
Message 1 of 8

How to use Python to implement multiple windows switching for a plugin.

zhujianchen
Contributor
Contributor

Hi,Guys.

When I import a plugin, I need to click the button in the main window to switch to another window. However, I have  completed displaying the main window , when I click the main window button, another window appears below the main window, rather than a separate window. Could anyone help me? Thanks very very much.

Below is the code I wrote:

from PySide2 import QtCore, QtGui, QtWidgets
import uiTools

form2, base2 = uiTools.loadUiType('Ui2.ui')
class Window_2(form2, base2):

def __init__(self, parent=None):
super(Window_2, self).__init__(parent)
parent.layout().addWidget(self)
self.parent = parent
self.setupUi(self)

self.setupUserInterface()

def setupUserInterface(self):
"""Setup and connect the plugins user interface"""


form, base = uiTools.loadUiType('UI1.ui')

class Main_window(form, base):

def __init__(self, parent=None):
super(Main_window, self).__init__(parent)
parent.layout().addWidget(self)
self.parent = parent
self.setupUi(self)

self.setupUserInterface()

def setupUserInterface(self):
"""Setup and connect the plugins user interface"""
self.switch_button.clicked.connect(self.creat_window_2)
def creat_window_2(self):
window_2 = Window_2(VREDPluginWidget)

main_window = Main_window(VREDPluginWidget)

 

0 Likes
Accepted solutions (3)
769 Views
7 Replies
Replies (7)
Message 2 of 8

andreasK3K4G
Enthusiast
Enthusiast
Accepted solution

Hi,

set window_2 as a new MainWindow.
Testet in VRED 2024. (for sure this code is for older version, cause of PySide2 instead of PySide6)

 

from PySide2 import QtCore, QtGui, QtWidgets
import uiTools

form2, base2 = uiTools.loadUiType('Ui2.ui')
class Window_2(form2, base2):
    def __init__(self, parent=None):
        super(Window_2, self).__init__(parent)
        self.setupUi(self)

    def setupUserInterface(self):
        """Setup and connect the plugins user interface"""

form, base = uiTools.loadUiType('UI1.ui')

class Main_window(form, base):
    def __init__(self, parent=None):
        super(Main_window, self).__init__(parent)
        self.setupUi(self)
        self.setupUserInterface()

    def setupUserInterface(self):
        """Setup and connect the plugins user interface"""
        self.switch_button.clicked.connect(self.creat_window_2)

    def creat_window_2(self):
        self.window_2 = QtWidgets.QMainWindow()
        self.window_2.setCentralWidget(Window_2(self.window_2))
        self.window_2.show()

main_window = Main_window(VREDPluginWidget)

 

Message 3 of 8

zhujianchen
Contributor
Contributor

Thank you very much for your reply. Your answer is very useful for me.

But, i have another question that when i want to get a value from window_2, the script report an error as below.

 

Traceback (most recent call last):

File "<string>", line 45, in Change_Value

AttributeError: 'PySide2.QtWidgets.QMainWindow' object has no attribute 'vaule_2'    ( 'vaule_2' is a DoubleSpinBox in window2)

 

Do you know how to salve the problem? Or,do you know how to exchange variables between the MainWindow and window_2?

 

Look forward to your reply,and thaks again.

 

Script:

from PySide2 import QtCore, QtGui, QtWidgets
import uiTools

form2, base2 = uiTools.loadUiType('UI2.ui')
class Window_2(form2, base2):
    def __init__(self, parent=None):
        super(Window_2, self).__init__(parent)
        parent.layout().addWidget(self)
        self.parent = parent
        self.setupUi(self)

        self.setupUserInterface()

    def setupUserInterface(self):
        """Setup and connect the plugins user interface"""
        self.pushButton.clicked.connect(self.change)

    def change(self):
        value = self.vaule_2.value()
        self.label_2.setText(str(value))


form, base = uiTools.loadUiType('main_window.ui')
class Main_Window(form, base):

    def __init__(self, parent=None):
        super(Main_Window, self).__init__(parent)
        parent.layout().addWidget(self)
        self.parent = parent
        self.setupUi(self)

        self.window_2 = QtWidgets.QMainWindow()
        self.setupUserInterface()

    def setupUserInterface(self):
        """Setup and connect the plugins user interface"""
        self.switch_button.clicked.connect(self.creat_window_2)
        self.change_value.clicked.connect(self.Change_Value)

    def creat_window_2(self):
        self.window_2.setCentralWidget(Window_2(self.window_2))
        self.window_2.show()

    def Change_Value(self):
        value = self.window_2.vaule_2.value()
        self.label_1.setText(str(value))


main_window = Main_Window(VREDPluginWidget)

main window:

zhujianchen_0-1719997527622.png

 

window_2:

zhujianchen_1-1719997586628.png

 

 

0 Likes
Message 4 of 8

andreasK3K4G
Enthusiast
Enthusiast
Accepted solution

Oh, sure, we need first create an instance of the class "Window_2".

Then we put this class as instance to setCentralWidget.

Now we can access the window2_class everytime we want to get or set values/properties.

 

You can do with @property like in line 12 till 19. (getter and setter).

Or you can work with "normal" function calls.

 

I renamed the spinbox to "value_2_spinbox", this helps to indentify the UI elements more easy.

 

 

from PySide2 import QtCore, QtGui, QtWidgets
import uiTools

form2, base2 = uiTools.loadUiType('Ui2.ui')
class Window_2(form2, base2):
    def __init__(self, parent=None):
        super(Window_2, self).__init__(parent)
        self.setupUi(self)
        self.value_2_spinbox.setValue(1)

    #work with property to get and set
    @property
    def value2(self):
        return (self.value_2_spinbox.value())

    @value2.setter
    def value2(self, value: int):
        print("try to set" + str(value))
        self.value_2_spinbox.setValue(value)

    #or work with function
    def just_a_function(self):
        string = "function called" + str(self.value_2_spinbox.value())
        return (string)

    def set_function(self, value):
        print("function call, set" + str(value))
        self.value_2_spinbox.setValue(int(value))

form, base = uiTools.loadUiType('UI1.ui')
class Main_window(form, base):
    def __init__(self, parent=None):
        super(Main_window, self).__init__(parent)
        self.setupUi(self)
        self.setupUserInterface()
        self.window2_class = None

    def setupUserInterface(self):
        """Setup and connect the plugins user interface"""
        self.switch_button.clicked.connect(self.creat_window_2)
        self.get_value_button.clicked.connect(self.get_value)
        self.set_value_button.clicked.connect(self.set_value)

    def creat_window_2(self):
        self.window2_class = Window_2()
        self.window_2 = QtWidgets.QMainWindow()
        self.window_2.setCentralWidget(self.window2_class)
        self.window_2.show()

    def get_value(self):
        if self.window2_class:
            print(self.window2_class.value2)    #with the property
            print(self.window2_class.just_a_function()) #with the function

    def set_value(self):
        if self.window2_class:
            self.window2_class.value2 = 5   #with the property
            self.window2_class.set_function(5) #with the function

main_window = Main_window(VREDPluginWidget)

 

 

Message 5 of 8

zhujianchen
Contributor
Contributor
Hi, thank you very much, your answer helpe me solve a big problem and really useful to me.
0 Likes
Message 6 of 8

zhujianchen
Contributor
Contributor

Hi,

Sorror, l have one more question. When l run the script, it report an error(Vred2024.1):

 

Traceback (most recent call last):

File "<string>", line 53, in get_value

AttributeError: 'PySide6.QtWidgets.QMainWindow' object has no attribute 'value2'

 

The error means that i can't exchange value between Main_window and window2. Do you know how to fix that?

 

Looking forward to your reply.

0 Likes
Message 7 of 8

andreasK3K4G
Enthusiast
Enthusiast
Accepted solution

Still? No, it is working for me.
Just in case, if you want the other way around, you need to put an instance of the parent to the windows2 class. Line 48 with "self".
Then change Line 6 and put "_parent" variable there, also for Line 7. Add it to a variable of Window_2 class, Line 10.

Now you can access the variable from MainWindow inside Window2 with self.__parentGUI.your_var like in Line 24.

 

from PySide6 import QtCore, QtGui, QtWidgets
import uiTools

form2, base2 = uiTools.loadUiType('Ui2.ui')
class Window_2(form2, base2):
    def __init__(self, _parent):
        super(Window_2, self).__init__(_parent)
        self.setupUi(self)
        self.value_2_spinbox.setValue(1)
        self.__parentGUI = _parent

    #work with property to get and set
    @property
    def value2(self):
        return (self.value_2_spinbox.value())

    @value2.setter
    def value2(self, value: int):
        print("try to set" + str(value))
        self.value_2_spinbox.setValue(value)

    #or work with function
    def just_a_function(self):
        string = "function called: " + str(self.value_2_spinbox.value()) + "parent value: " + str(self.__parentGUI.test)
        return (string)

    def set_function(self, value):
        print("function call, set" + str(value))
        self.value_2_spinbox.setValue(int(value))
        

form, base = uiTools.loadUiType('UI1.ui')
class Main_window(form, base):
    def __init__(self, parent=None):
        super(Main_window, self).__init__(parent)
        self.setupUi(self)
        self.setupUserInterface()
        self.window2_class = None
        self.test = "testvalue 3"

    def setupUserInterface(self):
        """Setup and connect the plugins user interface"""
        self.switch_button.clicked.connect(self.creat_window_2)
        self.get_value_button.clicked.connect(self.get_value)
        self.set_value_button.clicked.connect(self.set_value)

    def creat_window_2(self):
        self.window2_class = Window_2(self)
        self.window_2 = QtWidgets.QMainWindow()
        self.window_2.setCentralWidget(self.window2_class)
        self.window_2.show()

    def get_value(self):
        if self.window2_class:
            print(self.window2_class.value2)    #with the property
            print(self.window2_class.just_a_function()) #with the function

    def set_value(self):
        if self.window2_class:
            self.window2_class.value2 = 5   #with the property
            self.window2_class.set_function(5) #with the function

main_window = Main_window(VREDPluginWidget)
0 Likes
Message 8 of 8

zhujianchen
Contributor
Contributor

Ok, thank you very very much.

0 Likes