英文:
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())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论