PyQt:将用户输入添加到列表中 [英] PyQt: Adding user input into list

查看:131
本文介绍了PyQt:将用户输入添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个列表框,用户可以在其中向列表中输入更多项目.我有一个带有添加按钮的列表框.添加按钮会打开一个用户输入框,它将引导用户输入一个值.但是,我在将用户输入的值传递到添加到列表时遇到问题.任何建议都会有所帮助.下面是我的代码:

I am trying to make a list box where a user can enter more items to the list. I have a list box set up with an add button. The add button opens up a user input box where it will direct the user to enter a value. However, I am have issue with passing on the value of user input to the add onto the list. Any suggestions would help. Below is my code:

列表框

from input_box import *

class list_form(QtGui.QWidget):

    def __init__(self,list_of_items,open_text,parent= None):
        super(list_form, self).__init__()

        global output_path
        output_path = output_path_i

        grid = QtGui.QGridLayout()
        grid.setSpacing(10)
        self.widget = QtGui.QWidget()

        self.layout = QtGui.QGridLayout(self.widget)
        open_message = QtGui.QLabel(open_text)
        grid.addWidget(open_message,0,0,2,4)

        self.lst = QtGui.QListWidget()
        grid.addWidget(self.lst,3, 0,1,4)

        for i in list_of_items:
            self.lst.addItem(str(i))

        self.setLayout(grid)

        add = QtGui.QPushButton('Add')
        grid.addWidget(add,50,0)
        add.clicked.connect(self.add_button)

    def add_button(self):
        self.input_box = input_box()
        self.input_box.setWindowTitle("Window 2")
        self.input_box.show()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = list_form(list(xrange(100)),"List of Values")
    window.setWindowTitle('Window 1')
    window.show()
    sip.setdestroyonexit(False)
    sys.exit(app.exec_())

输入框

import sys
from PyQt4 import QtCore, QtGui
import sip

class input_box(QtGui.QWidget):

    def __init__(self,parent= None):
        super(input_box, self).__init__()

        grid = QtGui.QGridLayout()
        grid.setSpacing(10)
        self.widget = QtGui.QWidget()

        self.layout = QtGui.QGridLayout(self.widget)
        open_message = QtGui.QLabel("Enter Value:")
        grid.addWidget(open_message,0,0,2,3)

        self.txt = QtGui.QLineEdit()
        grid.addWidget(self.txt,2, 0,1,2)

        self.setLayout(grid)

        save = QtGui.QPushButton('Save')
        grid.addWidget(save,50,0)
        a = save.clicked.connect(self.save)
        save.clicked.connect(self.close)

        cancel = QtGui.QPushButton('Cancel')
        grid.addWidget(cancel,50,1)
        cancel.clicked.connect(self.close)


    def save(self):
        value = self.txt.text()
        return value

推荐答案

如果你想从窗口获取数据,你应该使用 QDialog 而不是 QWidget,将保存和取消的点击信号连接到接受和拒绝插槽,分别为:

If you want to get data from a window you should use a QDialog instead of a QWidget, connect the clicked signal of save and cancel to the accept and reject slot, respectively:

input_box.py

from PyQt4 import QtCore, QtGui

class Input_Box(QtGui.QDialog):
    def __init__(self,parent= None):
        super(Input_Box, self).__init__(parent)

        open_message = QtGui.QLabel("Enter Value:")
        self.txt = QtGui.QLineEdit()
        save = QtGui.QPushButton('Save', clicked=self.accept)
        cancel = QtGui.QPushButton('Cancel', clicked=self.reject)

        grid = QtGui.QGridLayout(self)
        grid.setSpacing(10)
        grid.addWidget(open_message, 0, 0)
        grid.addWidget(self.txt, 1, 0, 1, 2)
        grid.addWidget(save, 2, 0)
        grid.addWidget(cancel, 2, 1)
        self.setFixedSize(self.sizeHint())

    def save(self):
        value = self.txt.text()
        return value

然后使用 exec_() 方法,如果调用接受或拒绝,则返回代码,并根据该方法必须获取和添加数据.另一方面,不要使用全局变量,因为它们在调试时很头疼.

Then you use the exec_() method that returns a code if it is called accept or reject, and according to that the data must be obtained and added. On the other hand, do not use global variables because they are a headache when debugging.

from PyQt4 import QtCore, QtGui
from input_box import Input_Box

class list_form(QtGui.QWidget):
    def __init__(self,list_of_items,open_text,parent= None):
        super(list_form, self).__init__()

        open_message = QtGui.QLabel(open_text)
        self.lst = QtGui.QListWidget()
        self.lst.addItems([str(i) for i in list_of_items])
        add = QtGui.QPushButton('Add', clicked=self.add_button)
        grid = QtGui.QGridLayout(self)
        grid.setSpacing(10)
        grid.addWidget(open_message)
        grid.addWidget(self.lst)
        grid.addWidget(add)

    @QtCore.pyqtSlot()
    def add_button(self):
        input_box = Input_Box()
        input_box.setWindowTitle("Window 2")
        if input_box.exec_() == QtGui.QDialog.Accepted:
            val = input_box.save()
            it = QtGui.QListWidgetItem(val)
            self.lst.addItem(it)
            self.lst.scrollToItem(it)

if __name__ == "__main__":
    import sys
    import sip
    app = QtGui.QApplication(sys.argv)
    window = list_form(list(range(100)),"List of Values")
    window.setWindowTitle('Window 1')
    window.show()
    sip.setdestroyonexit(False)
    sys.exit(app.exec_())

使用 QDialog 的优点是类之间的解耦,例如不需要传递 QListWidget,因为另一个答案表明您可以将相同的对话框用于其他目的.

The advantage of using QDialog is the decoupling between the classes, for example there is no need to pass a QListWidget as the other answer suggests making it possible that you can use the same dialog for other purposes.

这篇关于PyQt:将用户输入添加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆