PyQt4中的多个类 [英] multiple classes in PyQt4

查看:458
本文介绍了PyQt4中的多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习Python基础知识后,我现在使用PyQt4在GUI中尝试自己。不幸的是,我现在坚持找出如何使用多个类,花了很多时间,试图在网上得到答案,而不是真正找到正确的答案,我希望你现在可以帮助我。

After learning the Python basics I'm now trying myself in GUI using PyQt4. Unfortunately I'm now stuck figuring out how to use multiple classes and after spending a lot of time trying to get the answer online and not really finding the right answer I hope you can now help me.

这是我的示例代码:

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        self.b1 = QtGui.QPushButton("Button", self)
        self.b1.move(100,100)


        self.setGeometry(300,300,200,200)
        self.setWindowTitle("Example")
        self.show()

class Bar(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.initUI()

    def initUI(self):

        self.statusBar().showMessage("Statusbar")


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())

if __name__ == "__main__":
    main()

现在只有Example但不是来自Bar类的statusBar。那么我怎么才能同时使用这两个类呢?一个人必须继承另一个东西吗?对不起,如果这可能很笨拙,有很多错误,但谢谢,如果你可以帮助我!

Right now only the Button from the "Example" class shows up but not the statusBar from the "Bar" class. So how exactly can I use both classes simultaneously? Does one have to inherit something from the other? Sorry if this might be very clumsy and have a lot of mistakes but thanks if you can help me!

推荐答案

a Bar 对象,并调用 show 方法:

You need to instantiate a Bar object, and call its show method:

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    bar = Bar()
    bar.show()
    sys.exit(app.exec_())




b
$ b

如果你想在一个窗口中的按钮和状态栏,将所有的小部件放在 QMainWindow

import sys
from PyQt4 import QtGui, QtCore

class Bar(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.initUI()

    def initUI(self):
        self.setGeometry(300,300,200,200)
        self.b1 = QtGui.QPushButton("Button", self)
        self.b1.move(100,100)
        self.setWindowTitle("Example")
        self.statusBar().showMessage("Statusbar")

def main():
    app = QtGui.QApplication(sys.argv)
    bar = Bar()
    bar.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

这篇关于PyQt4中的多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆