简体   繁体   English

打开后,Pyside窗口立即关闭

[英]Pyside window closes immediately after opening

I've been able to create pyside UIs before using this method, but for some reason for this time it wont stay open. 在使用此方法之前,我已经能够创建pyside UI,但是由于某种原因,它暂时不会保持打开状态。 It seems to flicker on quickly and then closes. 它似乎快速闪烁然后关闭。 I believe that it would be the trash colector that delets the app for not being used but I'm not sure as to how I can use the app more. 我相信是垃圾收集器会删除未使用的应用程序,但不确定如何使用该应用程序。

# coding: iso8859-1
import locale
import views
from PySide import QtGui
import PySide
from PySide import QtCore
import sys

locale.setlocale(locale.LC_CTYPE, '')
class gui(QtGui.QWidget):

    def __init__(self):
        super(gui, self).__init__()
        print "inside __init__"
        self.initUI()

    def initUI(self):
        self.window = QtGui.QMainWindow(self)
        self.grid = QtGui.QGridLayout()
        self.menuBTN = QtGui.QToolButton()
        self.menuBTN.setText("MENU")
        self.menuBTN.setMinimumSize(QtCore.QSize(100, 100))
        self.grid.addWidget(self.menuBTN, 0, 0, 1, 1)

        self.setLayout(self.grid)
        self.setWindowTitle('Office Works')
        self.showFullScreen()

    def change_layout(self):
        '''
        Method for saving the current view class and
        loading the view requested by the user. this
        method can be called by any of the view classes
        and it's functions to be able to change the screen
        when the user clicks on any if it's list objects or
        buttons

        '''
        pass
        blah=()
        class_temp = views.billing(blah)
        self.grid.addLayout(class_temp.grid, 0, 1)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    gui()
    print "executed gui"
    sys.exit(app.exec_())

You don't set any variable to the gui. 您没有为gui设置任何变量。

window = gui()
window.show()

Typically you want the main window as the main object. 通常,您希望将主窗口作为主要对象。 So gui might want to inherit QMainWindow. 因此,gui可能想要继承QMainWindow。

class Gui(QtGui.QMainWindow):

    def __init__(self):
        super(Gui, self).__init__()

        self.widget = QtGui.QWidget()
        self.grid = QtGui.QGridLayout()
        self.widget.setLayout(self.grid

        self.setCentralWidget(self.widget)
        ...

Another thing you can do is to add all of the items to the layout and simply hide/show the ones you want to use. 您可以做的另一件事是将所有项目添加到布局中,然后简单地隐藏/显示要使用的项目。 This might be easier than creating another view when a button is clicked. 这比单击按钮时创建另一个视图要容易。

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

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