简体   繁体   English

如何使用 QUiLoader() 和 pyside2 打开新的 window

[英]how to open new window using QUiLoader() with pyside2

I have a main window desiged with qtdesigner.我有一个使用 qtdesigner 设计的主要 window。 when I press a button, I would like to open a new window.当我按下一个按钮时,我想打开一个新的 window。 To do that, I was thinking it was possible to use the Qt loader function and I wrote this function (this function is executed when I press a button in my main window). To do that, I was thinking it was possible to use the Qt loader function and I wrote this function (this function is executed when I press a button in my main window).

def open_new_window(self):
    ui_file = QFile('gui/new_window.ui')
    ui_file.open(QFile.ReadOnly)
    loader = QUiLoader()
    self.window = loader.load(ui_file)
    ui_file.close()

    return

but when I do that, It close the program.但是当我这样做时,它会关闭程序。 Any Idea on what's wrong?关于有什么问题的任何想法? Can I use the loader function to open my uifile?我可以使用加载器 function 打开我的 uifile 吗? It is working for personalized widget import, why not for a new window?它适用于个性化小部件导入,为什么不适用新的 window?

edit:编辑:

I changed the function a litle bit:我稍微改变了 function :

def open_new_window(self):

    ui_file = QFile('gui/new_window.ui')
    ui_file.open(QFile.ReadOnly)  
    loader = QUiLoader()
    window = loader.load(ui_file)
    window.show()
    ui_file.close()

    return

This time the 2nd window is opening, but closing instantaneously...这次第 2 个 window 正在打开,但瞬间关闭......

Ok, I got it.好,我知道了。 The right code is:正确的代码是:

def open_new_window(self):

    ui_file = QFile('gui/new_window.ui')
    ui_file.open(QFile.ReadOnly)  
    loader = QUiLoader()
    self.window2 = loader.load(ui_file)
    self.window2.show()
    ui_file.close()

    return

What I changed: - I insert the "self."我改变了什么: - 我插入了“自我”。 in front of the window call.在 window 调用之前。 Without that, the window close immediately because the function is ended.否则,window 将立即关闭,因为 function 已结束。 With the self it remains alive.有了自我,它仍然活着。 - I also changed the name of the window in "window2". - 我还在“window2”中更改了 window 的名称。 The reason is that when I call my main window, I already used "window".原因是当我调用我的主 window 时,我已经使用了“窗口”。 As a result it replace the previous one.结果,它取代了前一个。 with a new name, both stay open!有了新名字,都保持开放!

regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM