python - 输入数据后 QComboxBox 值没有改变

标签 python pyqt pyqt4 qcombobox

我的QComboBox在输入数据后没有更改/刷新。

我应该添加什么来刷新QComboBox

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 = [] #----- object to load

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)

##################################
app = QApplication(sys.argv)
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)  # Didn't load after input the data

############################## 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_())

最佳答案

出现问题的原因是,当我使用 addItems 函数时,它会复制列表中的值,但不会保存列表中的引用。在您的情况下,无需将数据保存在列表中,只需使用 addItem 将其添加到 QCombobox 即可:

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)

完整代码:

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 = [] #----- object to load

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)

##################################
app = QApplication(sys.argv)
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)  # Didn't load after input the data

############################## 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_())

关于python - 输入数据后 QComboxBox 值没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44283736/

相关文章:

python - PyQt 应用程序与 sqlalchemy 数据库

javascript - 我在 Flask 应用程序中使用 $.(get) 请求时遇到的 JavaScript 问题

python - 无法在 python3 中导入 pymysql 模块

python - 值错误 : unconverted data remains on Pandas DataFrame

python - Python 的高效图像缩略图控件?

python - PyQT4 Combobox 更改另一个组合框的列表

python - 如何在 YAML 中指定始终使用 dictConfig 在项目文件夹中创建日志文件?

python - 在python程序中使用DBus

python - 如何等待强制子例程在继续下一行之前显示小部件

python - 如何在 PyQt 中创建可折叠框