PyQt Input Multiple Values in One Line












1















I want the user to input a series of values separated by blanks (or commas) in one text edit or any other input widget in PyQt. then I would like to be able to use these values in calculations. Please see the picture which gives an example to what I am trying to do.



Example picture










share|improve this question





























    1















    I want the user to input a series of values separated by blanks (or commas) in one text edit or any other input widget in PyQt. then I would like to be able to use these values in calculations. Please see the picture which gives an example to what I am trying to do.



    Example picture










    share|improve this question



























      1












      1








      1








      I want the user to input a series of values separated by blanks (or commas) in one text edit or any other input widget in PyQt. then I would like to be able to use these values in calculations. Please see the picture which gives an example to what I am trying to do.



      Example picture










      share|improve this question
















      I want the user to input a series of values separated by blanks (or commas) in one text edit or any other input widget in PyQt. then I would like to be able to use these values in calculations. Please see the picture which gives an example to what I am trying to do.



      Example picture







      python pyqt






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 27 '18 at 3:38









      eyllanesc

      84.7k103562




      84.7k103562










      asked Dec 27 '18 at 2:42









      abduallah adiabduallah adi

      124




      124
























          1 Answer
          1






          active

          oldest

          votes


















          1














          In general the idea is to use a QLineEdit and to prevent the user from entering inappropriate values using a QRegExpValidator, for example assuming that you only want floating values to be entered, the following example only allows entering that type of values, spaces and commas by calculating the sum and showing the values in a QListWidget:



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Widget(QtWidgets.QWidget):
          def __init__(self, parent=None):
          super(Widget, self).__init__(parent)
          flay = QtWidgets.QFormLayout(self)

          regex = r"^(s*(-|+)?d+(?:.d+)?s*,s*)+(-|+)?d+(?:.d+)?s*$"
          validator = QtGui.QRegExpValidator(QtCore.QRegExp(regex), self)
          self._le = QtWidgets.QLineEdit()
          self._le.setValidator(validator)

          self._list_widget = QtWidgets.QListWidget()

          button = QtWidgets.QPushButton("Press me")
          button.clicked.connect(self.on_clicked)

          self._result_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)

          flay.addRow("Input: ", self._le)
          flay.addRow("Output: ", self._list_widget)
          flay.addRow("Sum of values: ", self._result_label)
          flay.addRow(button)

          @QtCore.pyqtSlot()
          def on_clicked(self):
          self._list_widget.clear()
          if self._le.text():
          values = [float(val) for val in self._le.text().split(",")]
          print(values)
          self._list_widget.addItems([str(val) for val in values])
          self._result_label.setText(str(sum(values)))

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Widget()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer
























          • That is awesome!! thank you very much!

            – abduallah adi
            Dec 27 '18 at 15:47











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53939192%2fpyqt-input-multiple-values-in-one-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          In general the idea is to use a QLineEdit and to prevent the user from entering inappropriate values using a QRegExpValidator, for example assuming that you only want floating values to be entered, the following example only allows entering that type of values, spaces and commas by calculating the sum and showing the values in a QListWidget:



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Widget(QtWidgets.QWidget):
          def __init__(self, parent=None):
          super(Widget, self).__init__(parent)
          flay = QtWidgets.QFormLayout(self)

          regex = r"^(s*(-|+)?d+(?:.d+)?s*,s*)+(-|+)?d+(?:.d+)?s*$"
          validator = QtGui.QRegExpValidator(QtCore.QRegExp(regex), self)
          self._le = QtWidgets.QLineEdit()
          self._le.setValidator(validator)

          self._list_widget = QtWidgets.QListWidget()

          button = QtWidgets.QPushButton("Press me")
          button.clicked.connect(self.on_clicked)

          self._result_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)

          flay.addRow("Input: ", self._le)
          flay.addRow("Output: ", self._list_widget)
          flay.addRow("Sum of values: ", self._result_label)
          flay.addRow(button)

          @QtCore.pyqtSlot()
          def on_clicked(self):
          self._list_widget.clear()
          if self._le.text():
          values = [float(val) for val in self._le.text().split(",")]
          print(values)
          self._list_widget.addItems([str(val) for val in values])
          self._result_label.setText(str(sum(values)))

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Widget()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer
























          • That is awesome!! thank you very much!

            – abduallah adi
            Dec 27 '18 at 15:47
















          1














          In general the idea is to use a QLineEdit and to prevent the user from entering inappropriate values using a QRegExpValidator, for example assuming that you only want floating values to be entered, the following example only allows entering that type of values, spaces and commas by calculating the sum and showing the values in a QListWidget:



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Widget(QtWidgets.QWidget):
          def __init__(self, parent=None):
          super(Widget, self).__init__(parent)
          flay = QtWidgets.QFormLayout(self)

          regex = r"^(s*(-|+)?d+(?:.d+)?s*,s*)+(-|+)?d+(?:.d+)?s*$"
          validator = QtGui.QRegExpValidator(QtCore.QRegExp(regex), self)
          self._le = QtWidgets.QLineEdit()
          self._le.setValidator(validator)

          self._list_widget = QtWidgets.QListWidget()

          button = QtWidgets.QPushButton("Press me")
          button.clicked.connect(self.on_clicked)

          self._result_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)

          flay.addRow("Input: ", self._le)
          flay.addRow("Output: ", self._list_widget)
          flay.addRow("Sum of values: ", self._result_label)
          flay.addRow(button)

          @QtCore.pyqtSlot()
          def on_clicked(self):
          self._list_widget.clear()
          if self._le.text():
          values = [float(val) for val in self._le.text().split(",")]
          print(values)
          self._list_widget.addItems([str(val) for val in values])
          self._result_label.setText(str(sum(values)))

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Widget()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer
























          • That is awesome!! thank you very much!

            – abduallah adi
            Dec 27 '18 at 15:47














          1












          1








          1







          In general the idea is to use a QLineEdit and to prevent the user from entering inappropriate values using a QRegExpValidator, for example assuming that you only want floating values to be entered, the following example only allows entering that type of values, spaces and commas by calculating the sum and showing the values in a QListWidget:



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Widget(QtWidgets.QWidget):
          def __init__(self, parent=None):
          super(Widget, self).__init__(parent)
          flay = QtWidgets.QFormLayout(self)

          regex = r"^(s*(-|+)?d+(?:.d+)?s*,s*)+(-|+)?d+(?:.d+)?s*$"
          validator = QtGui.QRegExpValidator(QtCore.QRegExp(regex), self)
          self._le = QtWidgets.QLineEdit()
          self._le.setValidator(validator)

          self._list_widget = QtWidgets.QListWidget()

          button = QtWidgets.QPushButton("Press me")
          button.clicked.connect(self.on_clicked)

          self._result_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)

          flay.addRow("Input: ", self._le)
          flay.addRow("Output: ", self._list_widget)
          flay.addRow("Sum of values: ", self._result_label)
          flay.addRow(button)

          @QtCore.pyqtSlot()
          def on_clicked(self):
          self._list_widget.clear()
          if self._le.text():
          values = [float(val) for val in self._le.text().split(",")]
          print(values)
          self._list_widget.addItems([str(val) for val in values])
          self._result_label.setText(str(sum(values)))

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Widget()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer













          In general the idea is to use a QLineEdit and to prevent the user from entering inappropriate values using a QRegExpValidator, for example assuming that you only want floating values to be entered, the following example only allows entering that type of values, spaces and commas by calculating the sum and showing the values in a QListWidget:



          from PyQt5 import QtCore, QtGui, QtWidgets

          class Widget(QtWidgets.QWidget):
          def __init__(self, parent=None):
          super(Widget, self).__init__(parent)
          flay = QtWidgets.QFormLayout(self)

          regex = r"^(s*(-|+)?d+(?:.d+)?s*,s*)+(-|+)?d+(?:.d+)?s*$"
          validator = QtGui.QRegExpValidator(QtCore.QRegExp(regex), self)
          self._le = QtWidgets.QLineEdit()
          self._le.setValidator(validator)

          self._list_widget = QtWidgets.QListWidget()

          button = QtWidgets.QPushButton("Press me")
          button.clicked.connect(self.on_clicked)

          self._result_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)

          flay.addRow("Input: ", self._le)
          flay.addRow("Output: ", self._list_widget)
          flay.addRow("Sum of values: ", self._result_label)
          flay.addRow(button)

          @QtCore.pyqtSlot()
          def on_clicked(self):
          self._list_widget.clear()
          if self._le.text():
          values = [float(val) for val in self._le.text().split(",")]
          print(values)
          self._list_widget.addItems([str(val) for val in values])
          self._result_label.setText(str(sum(values)))

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = Widget()
          w.show()
          sys.exit(app.exec_())


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 27 '18 at 3:38









          eyllanesceyllanesc

          84.7k103562




          84.7k103562













          • That is awesome!! thank you very much!

            – abduallah adi
            Dec 27 '18 at 15:47



















          • That is awesome!! thank you very much!

            – abduallah adi
            Dec 27 '18 at 15:47

















          That is awesome!! thank you very much!

          – abduallah adi
          Dec 27 '18 at 15:47





          That is awesome!! thank you very much!

          – abduallah adi
          Dec 27 '18 at 15:47




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53939192%2fpyqt-input-multiple-values-in-one-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory