试图将 QLayout "" 添加到 QWidget "",它已经有一个布局
Posted
技术标签:
【中文标题】试图将 QLayout "" 添加到 QWidget "",它已经有一个布局【英文标题】:Attempting to add QLayout "" to QWidget "", which already has a layout 【发布时间】:2017-06-02 04:51:30 【问题描述】:我想将QWidget
添加到QGridLayout
时出错。
我是 PyQt 的新手,以前我不使用 Class 创建一个简单的 GUI,但现在我想尝试在 python 中使用类创建一个简单的 gui,虽然这让我感到困惑,但我得到了这个错误
QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout
QLayout::addChildLayout: layout "" already has a parent
这是我之前的无课作品
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyClass(object):
def __init__(self, device_type=None, ip=None, username=None, password=None, secret=None, command=None):
self.device_type = device_type
self.ip = ip
self.username = username
self.password = password
self.secret = secret
self.command = command
device_list = []
#ip_list = []
def addDevice():
device_type = str(cb_device_list.currentText())
ip = le_ip.text()
username = le_username.text()
password = le_password.text()
secret = le_enable.text()
command = 'show tech'
device_list.append(MyClass(device_type, ip, username, password, secret, command))
#ip_list.append(ip)
combobox_ip_list.addItem(ip)# Add Ip to ComboBox
for list in device_list:
print(list.device_type)
##################################
app = QApplication(sys.argv)
QApplication.processEvents()
app.setStyle('cleanlooks')
window = QWidget()
window.setWindowTitle("Network Automation")
############################# Input IP
# Device Type
lb_device_list = QLabel(window)
lb_device_list.setText('Device Type')
cb_device_list = QComboBox(window)
cb_device_list.addItem('cisco_ios')
cb_device_list.addItem('cisco_s300')
lb_ip = QLabel(window)
bt = QPushButton(window)
btadd = QPushButton(window)
# Ip Device
lb_ip.setText('IP Address')
le_ip = QLineEdit(window)
le_ip.setText('')
le_ip.setPlaceholderText('Input Device IP')
le_ip.setFixedWidth(150)
# username
lb_username = QLabel(window)
le_username = QLineEdit(window)
lb_username.setText('Username')
le_username.setText('')
le_username.setPlaceholderText('Input Username')
le_username.setFixedWidth(150)
# password
lb_password = QLabel(window)
le_password = QLineEdit(window)
lb_password.setText('Password')
le_password.setText('')
le_password.setPlaceholderText('Input Password')
le_password.setFixedWidth(150)
# Privilage Password
lb_enable = QLabel(window)
lb_enable.setText('Privilege Mode Password')
le_enable = QLineEdit(window)
le_enable.setText('')
le_enable.setPlaceholderText('Input Enable Password')
le_enable.setFixedWidth(150)
bt.setText('Generate')
bt.setFixedWidth(70)
btadd.setText('Add')
line = QFrame(window)
line.setFrameShape(QFrame.VLine)
line.setFrameShadow(QFrame.Sunken)
line.setLineWidth(3)
########################### Layout Ip Device List
lb3 = QLabel(window)
lb3.setText('IP Device List')
combobox_ip_list = QComboBox(window)
combobox_ip_list.setFixedWidth(170)
#combobox_ip_list.addItems(ip_list)
############################## SubLayout and Layout
hblayout = QHBoxLayout()
hblayout.addWidget(bt)
hblayout.addWidget(btadd)
sublayout = QVBoxLayout()
sublayout.addWidget(lb_device_list)
sublayout.addWidget(cb_device_list)
sublayout.addWidget(lb_ip)
sublayout.addWidget(le_ip)
sublayout.addWidget(lb_username)
sublayout.addWidget(le_username)
sublayout.addWidget(lb_password)
sublayout.addWidget(le_password)
sublayout.addWidget(lb_enable)
sublayout.addWidget(le_enable)
sublayout.addLayout(hblayout)
sublayout2 = QVBoxLayout()
sublayout2.addWidget(lb3)
sublayout2.addWidget(combobox_ip_list)
sublayout2.addStretch(1)
layout = QGridLayout(window)
layout.addLayout(sublayout,0,0)
layout.addWidget(line,0,1)
layout.addLayout(sublayout2,0,2)
btadd.clicked.connect(addDevice)
window.show()
sys.exit(app.exec_())
现在使用类(错误)
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Window(QMainWindow,QWidget):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle('Network Automation')
self.window = QWidget(self)
self.lb_device_list = QLabel('Device Type',self.window)
self.cb_device_list = QComboBox(self.window)
self.cb_device_list.addItem('cisco_ios')
self.cb_device_list.addItem('cisco_s300')
self.vbox = QVBoxLayout(self.window)
self.vbox.addWidget(self.lb_device_list)
self.vbox.addWidget(self.cb_device_list)
self.layout = QGridLayout(self.window)
self.layout.addLayout(self.vbox,0,0)
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = Window()
myapp.show()
sys.exit(app.exec_())
我需要像我以前使用类的工作一样创建简单的 gui
【问题讨论】:
【参考方案1】:放置some_layout(some_widget)
类似于some_widget.setLayout(some_layout)
,因此在每个布局中您都将其添加到self.window。
此外,MainWindow 已经有一个默认布局,因为它具有某些默认小部件,例如 QStatusBar 和 QMenubar,因此您不应该以这种方式添加它,您应该将它添加到 centralWidget,在您的情况下它可能是 self.window。
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
widget = QWidget(self)
self.setCentralWidget(widget)
self.setWindowTitle('Network Automation')
self.lb_device_list = QLabel('Device Type')
self.cb_device_list = QComboBox()
self.cb_device_list.addItem('cisco_ios')
self.cb_device_list.addItem('cisco_s300')
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.lb_device_list)
self.vbox.addWidget(self.cb_device_list)
self.layout = QGridLayout()
self.layout.addLayout(self.vbox,0,0)
widget.setLayout(self.layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = Window()
myapp.show()
sys.exit(app.exec_())
另一个观察,你不应该同时继承QMainWindow
和QWidget
,因为QMainWindow
继承自QWidget
,这是不必要的
另一种选择是实现一个继承自 QWidget 而不是 QMainWindow 的类。
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setWindowTitle('Network Automation')
self.lb_device_list = QLabel('Device Type')
self.cb_device_list = QComboBox()
self.cb_device_list.addItem('cisco_ios')
self.cb_device_list.addItem('cisco_s300')
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.lb_device_list)
self.vbox.addWidget(self.cb_device_list)
self.layout = QGridLayout()
self.layout.addLayout(self.vbox,0,0)
self.setLayout(self.layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = Window()
myapp.show()
sys.exit(app.exec_())
我还将您实现的代码转换为继承自 QWidget 的类:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
self.device_list = []
self.setWindowTitle("Network Automation")
############################# Input IP
# Device Type
lb_device_list = QLabel(self)
lb_device_list.setText('Device Type')
self.cb_device_list = QComboBox(self)
self.cb_device_list.addItem('cisco_ios')
self.cb_device_list.addItem('cisco_s300')
lb_ip = QLabel(self)
bt = QPushButton(self)
btadd = QPushButton(self)
# Ip Device
lb_ip.setText('IP Address')
self.le_ip = QLineEdit(self)
self.le_ip.setText('')
self.le_ip.setPlaceholderText('Input Device IP')
self.le_ip.setFixedWidth(150)
# username
lb_username = QLabel(self)
self.le_username = QLineEdit(self)
lb_username.setText('Username')
self.le_username.setText('')
self.le_username.setPlaceholderText('Input Username')
self.le_username.setFixedWidth(150)
# password
lb_password = QLabel(self)
self.le_password = QLineEdit(self)
lb_password.setText('Password')
self.le_password.setText('')
self.le_password.setPlaceholderText('Input Password')
self.le_password.setFixedWidth(150)
# Privilage Password
lb_enable = QLabel(self)
lb_enable.setText('Privilege Mode Password')
self.le_enable = QLineEdit(self)
self.le_enable.setText('')
self.le_enable.setPlaceholderText('Input Enable Password')
self.le_enable.setFixedWidth(150)
bt.setText('Generate')
bt.setFixedWidth(70)
btadd.setText('Add')
line = QFrame(self)
line.setFrameShape(QFrame.VLine)
line.setFrameShadow(QFrame.Sunken)
line.setLineWidth(3)
########################### Layout Ip Device List
lb3 = QLabel(self)
lb3.setText('IP Device List')
self.combobox_ip_list = QComboBox(self)
self.combobox_ip_list.setFixedWidth(170)
#combobox_ip_list.addItems(ip_list)
############################## SubLayout and Layout
hblayout = QHBoxLayout()
hblayout.addWidget(bt)
hblayout.addWidget(btadd)
sublayout = QVBoxLayout()
sublayout.addWidget(lb_device_list)
sublayout.addWidget(self.cb_device_list)
sublayout.addWidget(lb_ip)
sublayout.addWidget(self.le_ip)
sublayout.addWidget(lb_username)
sublayout.addWidget(self.le_username)
sublayout.addWidget(lb_password)
sublayout.addWidget(self.le_password)
sublayout.addWidget(lb_enable)
sublayout.addWidget(self.le_enable)
sublayout.addLayout(hblayout)
sublayout2 = QVBoxLayout()
sublayout2.addWidget(lb3)
sublayout2.addWidget(self.combobox_ip_list)
sublayout2.addStretch(1)
layout = QGridLayout(self)
layout.addLayout(sublayout,0,0)
layout.addWidget(line,0,1)
layout.addLayout(sublayout2,0,2)
btadd.clicked.connect(self.addDevice)
def addDevice(self):
device_type = str(self.cb_device_list.currentText())
ip = self.le_ip.text()
username = self.le_username.text()
password = self.le_password.text()
secret = self.le_enable.text()
command = 'show tech'
self.device_list.append(MyClass(device_type, ip, username, password, secret, command))
#ip_list.append(ip)
self.combobox_ip_list.addItem(ip)# Add Ip to ComboBox
for list in self.device_list:
print(list.device_type)
class MyClass(object):
def __init__(self, device_type=None, ip=None, username=None, password=None, secret=None, command=None):
self.device_type = device_type
self.ip = ip
self.username = username
self.password = password
self.secret = secret
self.command = command
#ip_list = []
##################################
app = QApplication(sys.argv)
QApplication.processEvents()
app.setStyle('cleanlooks')
window = Widget()
window.show()
sys.exit(app.exec_())
【讨论】:
以上是关于试图将 QLayout "" 添加到 QWidget "",它已经有一个布局的主要内容,如果未能解决你的问题,请参考以下文章
试图将 for /f "tokens=*" %%a in ... 转换为 SHELL