Pyside6将UI转换为py后,小部件为空。

huangapple go评论58阅读模式
英文:

Pyside6 after convert UI to py the widget is empty

问题

I am learning GUI using Qt Designer. After finishing the UI file, I used PySide6 to convert it to a .py file. When I tried to test the file, the widget is empty.

以下是您提供的Python代码的翻译部分:

import sys
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtCore import QFile
from ui_mainwindow import Ui_MainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

Ui文件: 您可以在此链接中找到UI文件 https://www.dropbox.com/s/bdhzy7a5jw143s1/login_window.ui?dl=0

Py文件: 您可以在此链接中找到.py文件 https://www.dropbox.com/s/ku0atiq2z040ywj/login_window.py?dl=0

英文:

İ am learning gui by qt designer, after finish the UI file, i used pyside6 to convert to py file.
Then when u try to test the file, the widget is empty!

Here is the code i try to import the py file.

import sys

from PySide6.QtWidgets import QApplication, QMainWindow

from PySide6.QtCore import QFile

from ui_mainwindow import Ui_MainWindow

class MainWindow(QMainWindow):

    def __init__(self):

        super(MainWindow, self).__init__()

        self.ui = Ui_MainWindow()

        self.ui.setupUi(self)

if __name__ == "__main__":

    app = QApplication(sys.argv)

    window = MainWindow()

    window.show()

    sys.exit(app.exec())

Ui file
https://www.dropbox.com/s/bdhzy7a5jw143s1/login_window.ui?dl=0

Py file
https://www.dropbox.com/s/ku0atiq2z040ywj/login_window.py?dl=0

答案1

得分: 0

问题出在你的主文件里,正如预期的那样:

from ui_mainwindow import Ui_w_loginwindow

在这里,你导入了类Ui_MainWindow,但是如果我们看一下ui_mainwindow.py,我们会发现类的名称实际上是Ui_w_loginwindow

class Ui_w_loginwindow(object):
    def setupUi(self, w_loginwindow):
        if not w_loginwindow.objectName():
            w_loginwindow.setObjectName(u"w_loginwindow")
        .
        .
        .
        .

所以你只需要导入正确的类名Ui_w_loginwindow,它就会正常运行:

import sys

from PySide6.QtWidgets import QApplication, QMainWindow

from PySide6.QtCore import QFile

from ui_mainwindow import Ui_w_loginwindow

class MainWindow(QMainWindow):

    def __init__(self):

        super(MainWindow, self).__init__()

        self.ui = Ui_w_loginwindow()

        self.ui.setupUi(self)

if __name__ == "__main__":

    app = QApplication(sys.argv)

    window = MainWindow()

    window.show()

    sys.exit(app.exec())
英文:

as expected the problem was in your main file :

from ui_mainwindow import Ui_MainWindow

here you imported the class Ui_MainWindow but if we took a look at ui_mainwindow.py we found that the name of the class is indeed Ui_w_loginwindow

class Ui_w_loginwindow(object):
    def setupUi(self, w_loginwindow):
        if not w_loginwindow.objectName():
            w_loginwindow.setObjectName(u"w_loginwindow")
        .
        .
        .
        .

so you have just to import the correct class name which is Ui_w_loginwindow and it will run just fine

import sys

from PySide6.QtWidgets import QApplication, QMainWindow

from PySide6.QtCore import QFile

from ui_mainwindow import Ui_w_loginwindow

class MainWindow(QMainWindow):

    def __init__(self):

        super(MainWindow, self).__init__()

        self.ui = Ui_w_loginwindow()

        self.ui.setupUi(self)

if __name__ == "__main__":

    app = QApplication(sys.argv)

    window = MainWindow()

    window.show()

    sys.exit(app.exec())

huangapple
  • 本文由 发表于 2023年5月14日 12:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定