当我想将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_())
现在使用Class (错误)
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,就像我之前的工作一样答案 0 :(得分:3)
放置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
,这是不必要的 < / p>
另一个选择是实现一个继承自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_())