首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用PyQt和Qt设计器ui文件

使用PyQt和Qt设计器ui文件
EN

Stack Overflow用户
提问于 2018-12-11 01:02:31
回答 1查看 0关注 0票数 0

我是PyQt的新手,我正在尝试直接从我的PyQt脚本中使用ui文件。我有两个ui文件,mainwindow.ui和landing.ui。单击主窗口上的“pushButton”按钮应打开着陆窗口。但是,单击按钮不能正常工作。这是代码(我只是试图解决问题,所以代码非常粗糙):

from PyQt4 import QtCore, uic
from PyQt4 import QtGui
import os

CURR = os.path.abspath(os.path.dirname('__file__'))

form_class = uic.loadUiType(os.path.join(CURR, "mainwindow.ui"))[0]
landing_class = uic.loadUiType(os.path.join(CURR, "landing.ui"))[0]

def loadUiWidget(uifilename, parent=None):
    uifile = QtCore.QFile(uifilename)
    uifile.open(QtCore.QFile.ReadOnly)
    ui = uic.loadUi(uifilename)
    uifile.close()
    return ui

@QtCore.pyqtSlot()
def clicked_slot():
    """this is called when login button is clicked"""
    LandingPage = loadUiWidget(os.path.join(CURR, "landing.ui"))
    center(LandingPage)
    icon(LandingPage)
    LandingPage.show()


class MyWindow(QtGui.QMainWindow, form_class):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(clicked_slot)

class LandingPage(QtGui.QMainWindow, landing_class):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)

def center(self):
    """ Function to center the application
    """
    qRect = self.frameGeometry()
    centerPoint = QtGui.QDesktopWidget().availableGeometry().center()
    qRect.moveCenter(centerPoint)
    self.move(qRect.topLeft())

def icon(self):
    """ Function to set window icon
    """
    appIcon = QtGui.QIcon("icon.png")
    self.setWindowIcon(appIcon)


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    pixmap = QtGui.QPixmap(os.path.join(CURR, "splash.png"))
    splash = QtGui.QSplashScreen(pixmap)
    splash.show()
    app.processEvents()    
    MainWindow = MyWindow(None)
    center(MainWindow)
    icon(MainWindow)
    MainWindow.show()
    splash.finish(MainWindow)
    sys.exit(app.exec_())

我哪里出错了?

EN

回答 1

Stack Overflow用户

发布于 2018-12-11 10:49:17

您的脚本有两个主要问题:首先,您没有正确构建ui文件的路径; 第二,你没有保留对登陆页面窗口的引用(因此它会在显示后立即被垃圾收集)。

以下是如何构建加载ui文件的脚本部分:

import os
from PyQt4 import QtCore, QtGui, uic

# get the directory of this script
path = os.path.dirname(os.path.abspath(__file__))

MainWindowUI, MainWindowBase = uic.loadUiType(
    os.path.join(path, 'mainwindow.ui'))

LandingPageUI, LandingPageBase = uic.loadUiType(
    os.path.join(path, 'landing.ui'))

class MainWindow(MainWindowBase, MainWindowUI):
    def __init__(self, parent=None):
        MainWindowBase.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.handleButton)

    def handleButton(self):
        # keep a reference to the landing page
        self.landing = LandingPage()
        self.landing.show()

class LandingPage(LandingPageBase, LandingPageUI):
    def __init__(self, parent=None):
        LandingPageBase.__init__(self, parent)
        self.setupUi(self)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档