如何将PySide框架与按钮布局结合起来
我看到一个关于pyside的脚本,演示了一个框架,还有一个脚本演示了按钮和盒子布局。
我尝试把这两个脚本结合起来,但我发现这两个功能不能同时工作:run_button_app(True)和run_frame_app(True)。
其中一个功能使用了这个语句:app_frame = QApplication([]),而另一个使用了这个语句:app1 = QtGui.QApplication(sys.argv)。我查的教程里没有解释这些内容。我已经订了一本关于Pyside和PyQt编程的书,但还没到。
我想弄明白如何使用框架(比如在窗口里放几个框架,把我的按钮放在其中一个框架里),但我还没搞明白,也不知道在哪里能找到相关的信息(例如示例?教程?)来学习如何使用框架。
我还是pyside和python的新手。
任何建议(比如相关教程的链接)都非常感谢。
谢谢,
Marc
"""
Cannibalized by Marc from
http://zetcode.com/gui/pysidetutorial/
ZetCode PySide tutorial author: Jan Bodnar website: zetcode.com
and
# explore QFrame() #http://www.daniweb.com/software-development/python/threads/366177
"""
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide import QtGui
initiate_app = False
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
# The following 4 lines are from "Window_with_frame.py"
# setGeometry(x_pos, y_pos, width, height)
# self.setGeometry(100, 150, width, height)
# self.setWindowTitle(title)
# self.make_frame()
self.initUI()
def initUI(self):
okButton = QtGui.QPushButton("OKh1_1")
cancelButton = QtGui.QPushButton("Cancel1_2")
ThirdButton = QtGui.QPushButton("Third1_3")
hbox2_1Button = QtGui.QPushButton("hbox2_Btn1")
hbox2_2Button = QtGui.QPushButton("hbox2_Btn2")
hbox3_1Button = QtGui.QPushButton("hbox3_Btn1")
hbox3_2Button = QtGui.QPushButton("hbox3_Btn2")
Vbox1Button = QtGui.QPushButton("Vbox1Button")
Vbox2Button = QtGui.QPushButton("Vbox2Button")
NewQqPtA1Button = QtGui.QPushButton("NewQqPtA1")
NewQqPtB2Button = QtGui.QPushButton("NewQqPtB2")
hbox1 = QtGui.QHBoxLayout()
hbox1.addStretch(1)
hbox1.addWidget(okButton)
hbox1.addWidget(cancelButton)
hbox1.addWidget(ThirdButton)
hbox2 = QtGui.QHBoxLayout()
hbox2.addStretch(1)
hbox2.addWidget(hbox2_1Button)
hbox2.addWidget(hbox2_2Button)
hbox1.addLayout(hbox2)
hbox3 = QtGui.QHBoxLayout()
hbox3.addStretch(1)
hbox3.addWidget(hbox3_1Button)
hbox3.addWidget(hbox3_2Button)
vbox1 = QtGui.QVBoxLayout()
vbox1.addStretch(1)
vbox1.addWidget(Vbox1Button)
vbox1.addLayout(hbox1)
#vbox1.addLayout(hbox2)
vbox1.addLayout(hbox3)
vbox2 = QtGui.QVBoxLayout()
vbox2.addStretch(1)
vbox2.addWidget(Vbox2Button)
vbox2.addLayout(vbox1)
self.setLayout(vbox2)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Buttons')
#self.make_frame()
self.show()
# The class is from "Window_with_frame.py"
class FrameTester(QWidget):
def __init__(self, title, width, height, parent=None):
# create the window (this will be instance self)
QWidget.__init__(self, parent)
# setGeometry(x_pos, y_pos, width, height)
self.setGeometry(100, 150, width, height)
self.setWindowTitle(title)
self.make_frame()
def make_frame(self):
frame = QFrame(self)
frame.setLineWidth(3)
frame.setFrameStyle(QFrame.Box|QFrame.Sunken)
# this will not have the effect you hope for
#frame.setFrameRect(QRect(10, 10, 20, 20))
layout = QBoxLayout(QBoxLayout.LeftToRight)
layout.addWidget(frame)
self.setLayout(layout)
def run_frame_app(initiate_app = False):
# create the Qt Application
if initiate_app == True:
app_frame = QApplication([])
title = "Window"
width = 800
height = 600
tester = FrameTester(title, width, height)
tester.show()
# run the main Qt event loop
app_frame.exec_()
def run_button_app(initiate_app = False):
# create the Qt Application
if initiate_app == True:
app1 = QtGui.QApplication(sys.argv)
ex = Example()
run_frame_app()
sys.exit(app1.exec_())
if __name__ == '__main__':
#run_button_app(True)
run_frame_app(True)
1 个回答
1
你不能同时运行两个 QApplication
实例,想了解更多可以看看这个 教程。
我不太明白你具体想做什么,但如果你想把 Example
这个小部件(包含按钮)添加到 FrameTester
中,你需要在 FrameTester
里创建一个布局,然后把 Example
的实例放到这个布局里(网上有很多相关的教程)。
如果你希望 Example
小部件有一个框架,你需要让它继承 QFrame
:
class Example(QFrame):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.setLineWidth(3)
self.setFrameStyle(QFrame.Box|QFrame.Sunken)
# ...
给你一个小建议,以后在定义某个 Widget
时,记得在 __init__
函数里提供 parent
参数,这样它可以在其他地方使用。
想了解更多,可以试试 zetcode 教程,还有 PySide 文档。