【问题标题】:Basic help needed in PyQt4PyQt4 所需的基本帮助
【发布时间】:2012-11-09 13:45:04
【问题描述】:

基本上我有一个带有菜单栏和许多选项的主窗口。当我单击其中一个选项时,我的代码应该会打开另一个窗口。我的代码现在看起来像这样。 已导入所有必需的库。

class subwindow(self):
    //Another small window

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow , self).__init__()     
        self.window()

    def window(self):
         Action = QtGui.QAction(QtGui.QIcon('action.png') , 'action' , self)          
         Action.triggered.connect(self.a)

         mb = self.menuBar()
         option = mb.addMenu('File')
         option.addAction(Action)

         self.show()

   def a(self):
         s = subwindow()



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    sys.exit(app.exec_())

如何运行代码的子窗口部分。如何添加 QtGui.QApplication 部分?

【问题讨论】:

  • 不必必须为每个窗口创建一个QApplicationQApplication 必须只有一个,因此您只需创建子窗口并调用 show 方法即可。

标签: python window pyqt pyqt4 qtgui


【解决方案1】:

就像@Bakuriu 在 cmets 中所说,QApplication 的实例必须只有一个。这将启动应用程序的主事件循环。

您可以通过从 QDialog 类派生您的 SubWindow 类并根据需要自定义它来创建新窗口。 您需要调用 QDialog 类的 exec_() 方法来显示对话框。

例如,在您的代码中:

from PyQt4 import QtGui
import sys

class SubWindow(QtGui.QDialog):
    def __init__(self):
        super(SubWindow , self).__init__()     
        label = QtGui.QLabel("Hey, subwindow here!",self);

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow , self).__init__()     
        self.window()

    def window(self):
        Action = QtGui.QAction(QtGui.QIcon('action.png') , 'action' , self)          
        Action.triggered.connect(self.a)

        mb = self.menuBar()
        option = mb.addMenu('File')
        option.addAction(Action)

        self.show()

    def a(self):

        s = SubWindow()
        s.exec_()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    sys.exit(app.exec_())

【讨论】:

    【解决方案2】:

    当您想在 Qt 应用程序和许多 gui 应用程序(如 GTK)中打开子窗口时,您可以打开一个对话框。下面的示例可以向您展示如何执行此操作。它有一个主窗口,其中有一个菜单,可以打开对话框并询问你的名字。如果您想自定义对话框,它使用内置对话框,它包含的内容可以查看Create a custom dialog。有关创建对话框而不是另一个 QMainWindow 的讨论,请查看Multiple Windows in PyQt4

    import sys
    from PyQt4 import QtGui
    
    class MainWindow(QtGui.QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            action = QtGui.QAction(QtGui.QIcon('action.png'), '&New Window', self)
            action.triggered.connect(self.new_win)
            self.menuBar().addMenu('&File').addAction(action)
            self.setGeometry(300, 300, 300, 200)
            self.show()
    
        def new_win(self):
            name, ok = QtGui.QInputDialog.getText(self, 'Input', 'Enter name:')
            print name, ok
    
    app = QtGui.QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      相关资源
      最近更新 更多