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()