我正在用PyQt编写应用程序。因为我应该坚持面向对象的编程,所以应该修复我的主文件。另外,因为我想使用信号和插槽,以后我会遇到一些麻烦,最好在现有代码变得更复杂之前修复它。
正如您在我的代码中看到的,我只有一个exec_()方法,但不在if __name__ == "__main__":
我不应该问这个,但在我当前的代码中,主要的小部件是一个继承QGraphicsView的自定义类。在我阅读的所有示例中,主类是QDialogs,QWidgets或QMainWindow,然后在if语句中执行应用程序。
这是我目前的代码:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from dragbutton import DragButton
from wiringgraphicsview import *
import collections
import icons_rc
app = QApplication([])
scene = MyScene()
menu = QMenu()
widget_container = QWidget()
#dictonaries for dragbuttons (used later for connecting them)
jacks_dic = {}
inputs_dic = collections.OrderedDict()
wire_dic = {}
#AIF1RX (PLAYBACK CONTROLS)
# put a button into the scene and move it
btn_AIF1RX1 = DragButton()
btn_AIF1RX1.setObjectName("btn_AIF1RX1")
btn_AIF1RX1.setAllowDrag(True) #Allow Drag n Drop of DragButton
btn_AIF1RX1.setAcceptDrops(False)
btn_AIF1RX1.setGeometry(QRect(-100, 50, 51, 31)) #Set dimensions of it
#Set icon of button1
icon = QIcon()
icon.addPixmap(QPixmap(":/audio-input-line.png"), QIcon.Normal, QIcon.Off)
btn_AIF1RX1.setIcon(icon)
btn_AIF1RX1.setFlat(False)
btn_AIF1RX1.setMenu(menu)
#jacks_array.append(btn_AIF1RX1)
btn_AIF1RX2 = DragButton()
btn_AIF1RX2.setObjectName("btn_AIF1RX2")
btn_AIF1RX2.setGeometry(QRect(200, -50, 51, 31)) #Set dimensions of it
btn_AIF1RX2.setAllowDrag(True) #Allow Drag n Drop of DragButton
btn_AIF1RX2.setAcceptDrops(False)
#Set input line icon
icon = QIcon()
icon.addPixmap(QPixmap(":/audio-input-line.png"), QIcon.Normal, QIcon.Off)
btn_AIF1RX2.setIcon(icon)
btn_AIF1RX2.setFlat(False)
btn_AIF1RX2.setMenu(menu)
#jacks_array.append(btn_AIF1RX2)
#Create Layouts for AIF1RX
AIF1RX_main_layout = QVBoxLayout()
AIF1RX_layout = QHBoxLayout()
AIF1RX1_layout = QVBoxLayout()
AIF1RX2_layout = QVBoxLayout()
#Create Labels for AIF1RXl
lbl_playback = QLabel("Playback From RPi")
lbl_AIF1RX1_L = QLabel("L")
AIF1RX1_layout.addWidget(lbl_AIF1RX1_L)
lbl_AIF1RX2_R = QLabel("R")
AIF1RX2_layout.addWidget(lbl_AIF1RX2_R)
lbl_AIF1RX1 = QLabel("AIF1RX1")
AIF1RX1_layout.addWidget( lbl_AIF1RX1 )
lbl_AIF1RX2 = QLabel("AIF1RX2")
AIF1RX2_layout.addWidget( lbl_AIF1RX2 )
#fit AIF1RX layouts
AIF1RX_main_layout.addWidget( lbl_playback )
AIF1RX_main_layout.addLayout(AIF1RX_layout)
AIF1RX_layout.addLayout(AIF1RX1_layout)
AIF1RX_layout.addLayout(AIF1RX2_layout)
AIF1RX1_layout.addWidget(btn_AIF1RX1)
AIF1RX2_layout.addWidget(btn_AIF1RX2)
#AIF1TX (RECORD INPUTS)
btn_AIF1TX1_1 = DragButton()
btn_AIF1TX1_1.setObjectName("btn_AIF1TX1_1")
#This button shoudn't be dragged, it is just for dropping.
btn_AIF1TX1_1.setAllowDrag(False)
btn_AIF1TX1_1.setAcceptDrops(True)
icon = QIcon()
icon.addPixmap(QPixmap(":/input_small.png"), QIcon.Normal, QIcon.Off)
btn_AIF1TX1_1.setIcon(icon)
btn_AIF1TX1_1.setFixedWidth(16)
inputs_dic['AIF1TX1_1'] = None
btn_AIF1TX1_2 = DragButton()
btn_AIF1TX1_2.setObjectName("btn_AIF1TX1_2")
btn_AIF1TX1_2.setAllowDrag(False)
btn_AIF1TX1_2.setAcceptDrops(True)
icon = QIcon()
icon.addPixmap(QPixmap(":/input_small.png"), QIcon.Normal, QIcon.Off)
btn_AIF1TX1_2.setIcon(icon)
btn_AIF1TX1_2.setFixedWidth(16)
inputs_dic['AIF1TX1_2'] = None
btn_AIF1TX1_3 = DragButton()
btn_AIF1TX1_3.setObjectName("btn_AIF1TX1_3")
btn_AIF1TX1_3.setAllowDrag(False)
btn_AIF1TX1_3.setAcceptDrops(True)
icon = QIcon()
icon.addPixmap(QPixmap(":/input_small.png"), QIcon.Normal, QIcon.Off)
btn_AIF1TX1_3.setIcon(icon)
btn_AIF1TX1_3.setFixedWidth(16)
inputs_dic['AIF1TX1_3'] = None
btn_AIF1TX1_4 = DragButton()
btn_AIF1TX1_4.setObjectName("btn_AIF1TX1_4")
btn_AIF1TX1_4.setAllowDrag(False)
btn_AIF1TX1_4.setAcceptDrops(True)
icon = QIcon()
icon.addPixmap(QPixmap(":/input_small.png"), QIcon.Normal, QIcon.Off)
btn_AIF1TX1_4.setIcon(icon)
btn_AIF1TX1_4.setFixedWidth(16)
inputs_dic['AIF1TX1_4'] = None
btn_AIF1TX2_1 = DragButton()
btn_AIF1TX2_1.setObjectName("btn_AIF1TX2_1")
btn_AIF1TX2_1.setAllowDrag(False)
btn_AIF1TX2_1.setAcceptDrops(True)
icon = QIcon()
icon.addPixmap(QPixmap(":/input_small.png"), QIcon.Normal, QIcon.Off)
btn_AIF1TX2_1.setIcon(icon)
btn_AIF1TX2_1.setFixedWidth(16)
inputs_dic['AIF1TX2_1'] = None
btn_AIF1TX2_2 = DragButton()
btn_AIF1TX2_2.setObjectName("btn_AIF1TX2_2")
btn_AIF1TX2_2.setAllowDrag(False)
btn_AIF1TX2_2.setAcceptDrops(True)
icon = QIcon()
icon.addPixmap(QPixmap(":/input_small.png"), QIcon.Normal, QIcon.Off)
btn_AIF1TX2_2.setIcon(icon)
btn_AIF1TX2_2.setFixedWidth(16)
inputs_dic['AIF1TX2_2'] = None
btn_AIF1TX2_3 = DragButton()
btn_AIF1TX2_3.setObjectName("btn_AIF1TX2_3")
btn_AIF1TX2_3.setAllowDrag(False)
btn_AIF1TX2_3.setAcceptDrops(True)
icon = QIcon()
icon.addPixmap(QPixmap(":/input_small.png"), QIcon.Normal, QIcon.Off)
btn_AIF1TX2_3.setIcon(icon)
btn_AIF1TX2_3.setFixedWidth(16)
inputs_dic['AIF1TX2_3'] = None
btn_AIF1TX2_4 = DragButton()
btn_AIF1TX2_4.setObjectName("btn_AIF1TX2_4")
btn_AIF1TX2_4.setAllowDrag(False)
btn_AIF1TX2_4.setAcceptDrops(True)
icon = QIcon()
icon.addPixmap(QPixmap(":/input_small.png"), QIcon.Normal, QIcon.Off)
btn_AIF1TX2_4.setIcon(icon)
btn_AIF1TX2_4.setFixedWidth(16)
inputs_dic['AIF1TX2_4'] = None
#Create Layouts for AIF1TX
AIF1TX_main_layout = QVBoxLayout()
AIF1TX_layout = QHBoxLayout()
AIF1TX1_layout = QVBoxLayout()
AIF1TX2_layout = QVBoxLayout()
AIF1TX1_inputs_layout = QHBoxLayout()
AIF1TX2_inputs_layout = QHBoxLayout()
#Create Labels for AIF1TX
lbl_record = QLabel("Record to RPi")
lbl_AIF1TX1_L = QLabel("L")
AIF1TX1_layout.addWidget(lbl_AIF1TX1_L)
lbl_AIF1TX2_R = QLabel("R")
AIF1TX2_layout.addWidget(lbl_AIF1TX2_R)
lbl_AIF1TX1 = QLabel("AIF1TX1")
AIF1TX1_layout.addWidget( lbl_AIF1TX1 )
lbl_AIF1TX2 = QLabel("AIF1TX2")
AIF1TX2_layout.addWidget( lbl_AIF1TX2 )
#fit AIF1TX layouts
AIF1TX_main_layout.addWidget( lbl_record )
AIF1TX_main_layout.addLayout(AIF1TX_layout)
AIF1TX_layout.addLayout(AIF1TX1_layout)
AIF1TX_layout.addLayout(AIF1TX2_layout)
AIF1TX1_layout.addLayout(AIF1TX1_inputs_layout)
AIF1TX2_layout.addLayout(AIF1TX2_inputs_layout)
#Inputs
AIF1TX1_inputs_layout.addWidget(btn_AIF1TX1_1)
AIF1TX1_inputs_layout.addWidget(btn_AIF1TX1_2)
AIF1TX1_inputs_layout.addWidget(btn_AIF1TX1_3)
AIF1TX1_inputs_layout.addWidget(btn_AIF1TX1_4)
AIF1TX1_inputs_layout.setSpacing(0)
AIF1TX1_inputs_layout.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint)
AIF1TX2_inputs_layout.addWidget(btn_AIF1TX2_1)
AIF1TX2_inputs_layout.addWidget(btn_AIF1TX2_2)
AIF1TX2_inputs_layout.addWidget(btn_AIF1TX2_3)
AIF1TX2_inputs_layout.addWidget(btn_AIF1TX2_4)
AIF1TX2_inputs_layout.setSpacing(0)
AIF1TX2_inputs_layout.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint)
#Wrap Playback/Record controls into a layout
record_playback_layouts = QHBoxLayout(widget_container)
record_playback_layouts.addLayout(AIF1RX_main_layout)
record_playback_layouts.addLayout(AIF1TX_main_layout)
#widget_container.setFixedWidth(290)
widget_scene = scene.addWidget(widget_container)
# Instantiate our own proxy which forwars drag/drop events to the child widget
#of AIFTX
proxy_btn_AIF1TX1_1 = ProxyWidget()
proxy_btn_AIF1TX1_1.setWidget(btn_AIF1TX1_1)
proxy_btn_AIF1TX1_1.setAcceptDrops(True)
scene.addItem(proxy_btn_AIF1TX1_1)
#
proxy_btn_AIF1TX1_2 = ProxyWidget()
proxy_btn_AIF1TX1_2.setWidget(btn_AIF1TX1_2)
proxy_btn_AIF1TX1_2.setAcceptDrops(True)
scene.addItem(proxy_btn_AIF1TX1_2)
proxy_btn_AIF1TX1_3 = ProxyWidget()
proxy_btn_AIF1TX1_3.setWidget(btn_AIF1TX1_3)
proxy_btn_AIF1TX1_3.setAcceptDrops(True)
scene.addItem(proxy_btn_AIF1TX1_3)
proxy_btn_AIF1TX1_4 = ProxyWidget()
proxy_btn_AIF1TX1_4.setWidget(btn_AIF1TX1_4)
proxy_btn_AIF1TX1_4.setAcceptDrops(True)
scene.addItem(proxy_btn_AIF1TX1_4)
proxy_btn_AIF1TX2_1 = ProxyWidget()
proxy_btn_AIF1TX2_1.setWidget(btn_AIF1TX2_1)
proxy_btn_AIF1TX2_1.setAcceptDrops(True)
scene.addItem(proxy_btn_AIF1TX2_1)
proxy_btn_AIF1TX2_2 = ProxyWidget()
proxy_btn_AIF1TX2_2.setWidget(btn_AIF1TX2_2)
proxy_btn_AIF1TX2_2.setAcceptDrops(True)
scene.addItem(proxy_btn_AIF1TX2_2)
proxy_btn_AIF1TX2_3 = ProxyWidget()
proxy_btn_AIF1TX2_3.setWidget(btn_AIF1TX2_3)
proxy_btn_AIF1TX2_3.setAcceptDrops(True)
scene.addItem(proxy_btn_AIF1TX2_3)
proxy_btn_AIF1TX2_4 = ProxyWidget()
proxy_btn_AIF1TX2_4.setWidget(btn_AIF1TX2_4)
proxy_btn_AIF1TX2_4.setAcceptDrops(True)
scene.addItem(proxy_btn_AIF1TX2_4)
@pyqtSlot(str)
def on_connect(self, input):
print 'connected'
jack_connector = self.sender().parent().objectName() #sender's parent of QAction should be the button
wire_dic['wire_1'] = Wire( jack_connector , widget_container.findChild( DragButton, 'btn_' + input ) , None, scene)
#Load Menu options for Jacks dragbuttons
#create sub-menus
submenus_dic = collections.OrderedDict()
submenus_dic['AIF1TX1_submenu'] = QMenu("AIF1TX1 (L) Record to RPi")
submenus_dic['AIF1TX2_submenu'] = QMenu("AIF1TX2 (R) Record to RPi")
actions_dic = collections.OrderedDict()
for input in inputs_dic:
#Create an Action
actions_dic[ input ] = QtGui.QAction( input, None)
actions_dic[ input ].triggered[()].connect( lambda input=input: on_connect(actions_dic[ input ], input) )
#Condition to add to a submenu
if input[:-2] == 'AIF1TX1' :
submenus_dic['AIF1TX1_submenu'].addAction( actions_dic[ input ] )
if input[:-2] == 'AIF1TX2' :
submenus_dic['AIF1TX2_submenu'].addAction( actions_dic[ input ] )
#Add SubMenus to Main Menu
for submenu in submenus_dic:
menu.addMenu(submenus_dic[ submenu ] )
# Create the view using the scene
view = WiringGraphicsView(None, scene)
view.show()
view.setWindowTitle("Device Advanced Mixer")
app.exec_()
答案 0 :(得分:0)
确定您的应用中可以有用地拆分成类的部分,然后执行此操作。
过去当面对将非OO转换为OO时,我发现最容易一个接一个地开始 - 不要试图一次性完成所有这一切 - 然后开始好好开始坐下来考虑这种方法是否最有意义。如果您可以看到多次使用相同代码模式的区域,请查看是否可以在基类中有用地共享它。但是,除非这些项目实际上是相关的,否则不要分享。记住它总是有用的。
如果:
请记住,没有'权利'方式,有方法可行。作为一名程序员,你的任务是找到有效的方式,不仅可以为你理解,也可以为其他人理解。
理想情况下,它既可扩展,又易于调试和优化,但随着时间的推移而来!
HTH