0
私はいくつかの項目を持つQListWidgetを持っています。実際にリスト(配列オブジェクト)とQlistWidgetから項目を削除するフォームの「削除」ボタンがあります。QListWidgetの項目インデックスがデフォルトで選択されています
配列objにアイテムを追加してQlistWidgetに表示するための「追加」ボタン。リストからアイテムを選択せずにremoveを押すと、デフォルトでlatsインデックスにあるアイテムが削除され、削除ボタンを押すとQlistWidgetが更新されませんでした。
私はそれを説明する方法を混同し、コード最初
を試してみる必要がある、これは私のコードです:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from netmiko import ConnectHandler
from paramiko import SSHException
import jtextfsm as textfsm
import ipaddress
import time
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
self.device_list = [] #object array
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')
# Ip Device
lb_ip = QLabel(self)
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)
#button generate and add
btgenerate = QPushButton(self)
btgenerate.setText('Generate')
btgenerate.setFixedWidth(70)
btadd = QPushButton(self)
btadd.setText('Add')
#button delete
btdel = QPushButton(self)
btdel.setFixedWidth(70)
btdel.setText('Remove')
# line
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.ip_device_list = QListWidget(self)
self.ip_device_list.setFixedWidth(150)
#self.combobox_ip_list = QComboBox(self)
#self.combobox_ip_list.setFixedWidth(170)
############################## SubLayout and Layout
hblayout = QHBoxLayout()
hblayout.addWidget(btgenerate)
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.ip_device_list)
#sublayout2.addWidget(self.combobox_ip_list)
sublayout2.addWidget(btdel)
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)
btdel.clicked.connect(self.remove)
####################----------- AddDevice
def addDevice(self):
try:
ip = self.le_ip.text()
ipaddress.ip_address(ip)
device_type = str(self.cb_device_list.currentText())
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))
# self.combobox_ip_list.addItem(ip)# Add Ip to ComboBox
self.ip_device_list.addItem(ip)
self.le_ip.clear()
self.le_username.clear()
self.le_password.clear()
self.le_enable.clear()
#self.ip_device_list.clear()
for list in self.device_list:
print(list.ip,list.device_type)
except ValueError:
print("insert you're ip correctly")
def remove(self):
index = self.ip_device_list.currentRow()
self.ip_device_list.takeItem(index)
del self.device_list[index] #dremove data from list/array
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
##################################
app = QApplication(sys.argv)
QApplication.processEvents()
app.setStyle('cleanlooks')
window = Widget()
window.show()
sys.exit(app.exec_())