Форум сайта python.su
Здравствуйте. Подскажите, пожалуйста, как можно текст из QLineEdit пихать циклом в каждый QLabel.
Есть 1 lineEdit и 8 label. Смысл в том, чтобы писать в lineEdit и нажимая enter, текст писался в label. После этого поле ввода обнуляется и следующий текст идет в следующий label.
У меня получилось только или в 1 лейбл все писать или сразу в несколько, но не по очереди. Многое перепробывал, но никак не пойму, как сделать цикл.
Код сгенерирован в дизайнере. Работают только строка ввода и 1й лейбл.
Если вместо нескольких лейблов выбираю textEdit, то все отлично добавляется без цикла.
Заранее спасибо.
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'hz2.ui' # # Created by: PyQt4 UI code generator 4.11.4 # # WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(800, 600) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth()) MainWindow.setSizePolicy(sizePolicy) MainWindow.setMinimumSize(QtCore.QSize(800, 600)) MainWindow.setMaximumSize(QtCore.QSize(800, 600)) MainWindow.setMouseTracking(False) MainWindow.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap(_fromUtf8("mc.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off) MainWindow.setWindowIcon(icon) MainWindow.setDocumentMode(False) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.btnToExcel = QtGui.QPushButton(self.centralwidget) self.btnToExcel.setGeometry(QtCore.QRect(50, 500, 101, 23)) font = QtGui.QFont() font.setPointSize(10) self.btnToExcel.setFont(font) self.btnToExcel.setStatusTip(_fromUtf8("")) self.btnToExcel.setAutoDefault(False) self.btnToExcel.setDefault(False) self.btnToExcel.setFlat(False) self.btnToExcel.setObjectName(_fromUtf8("btnToExcel")) self.btnBack = QtGui.QPushButton(self.centralwidget) self.btnBack.setGeometry(QtCore.QRect(650, 500, 101, 23)) font = QtGui.QFont() font.setPointSize(10) self.btnBack.setFont(font) self.btnBack.setObjectName(_fromUtf8("btnBack")) self.lineHor = QtGui.QFrame(self.centralwidget) self.lineHor.setGeometry(QtCore.QRect(0, 450, 801, 16)) self.lineHor.setFrameShape(QtGui.QFrame.HLine) self.lineHor.setFrameShadow(QtGui.QFrame.Sunken) self.lineHor.setObjectName(_fromUtf8("lineHor")) self.checkBox = QtGui.QCheckBox(self.centralwidget) self.checkBox.setGeometry(QtCore.QRect(30, 30, 281, 17)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(12) font.setStrikeOut(True) self.checkBox.setFont(font) self.checkBox.setAutoFillBackground(True) self.checkBox.setObjectName(_fromUtf8("checkBox")) self.checkBox_2 = QtGui.QCheckBox(self.centralwidget) self.checkBox_2.setGeometry(QtCore.QRect(30, 60, 281, 17)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(12) font.setStrikeOut(True) self.checkBox_2.setFont(font) self.checkBox_2.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu) self.checkBox_2.setCheckable(False) self.checkBox_2.setObjectName(_fromUtf8("checkBox_2")) self.checkBox_3 = QtGui.QCheckBox(self.centralwidget) self.checkBox_3.setGeometry(QtCore.QRect(30, 90, 281, 17)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(12) font.setBold(True) font.setWeight(75) self.checkBox_3.setFont(font) self.checkBox_3.setCheckable(False) self.checkBox_3.setAutoRepeat(False) self.checkBox_3.setObjectName(_fromUtf8("checkBox_3")) self.checkBox_4 = QtGui.QCheckBox(self.centralwidget) self.checkBox_4.setGeometry(QtCore.QRect(30, 120, 281, 17)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(12) self.checkBox_4.setFont(font) self.checkBox_4.setCheckable(False) self.checkBox_4.setObjectName(_fromUtf8("checkBox_4")) self.checkBox_5 = QtGui.QCheckBox(self.centralwidget) self.checkBox_5.setGeometry(QtCore.QRect(30, 150, 281, 17)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(12) self.checkBox_5.setFont(font) self.checkBox_5.setCheckable(False) self.checkBox_5.setObjectName(_fromUtf8("checkBox_5")) self.checkBox_6 = QtGui.QCheckBox(self.centralwidget) self.checkBox_6.setGeometry(QtCore.QRect(30, 180, 281, 17)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(12) self.checkBox_6.setFont(font) self.checkBox_6.setCheckable(False) self.checkBox_6.setChecked(False) self.checkBox_6.setObjectName(_fromUtf8("checkBox_6")) self.checkBox_7 = QtGui.QCheckBox(self.centralwidget) self.checkBox_7.setGeometry(QtCore.QRect(30, 210, 281, 17)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(12) self.checkBox_7.setFont(font) self.checkBox_7.setCheckable(False) self.checkBox_7.setObjectName(_fromUtf8("checkBox_7")) self.checkBox_8 = QtGui.QCheckBox(self.centralwidget) self.checkBox_8.setGeometry(QtCore.QRect(30, 240, 281, 17)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(12) self.checkBox_8.setFont(font) self.checkBox_8.setCheckable(False) self.checkBox_8.setAutoRepeat(False) self.checkBox_8.setObjectName(_fromUtf8("checkBox_8")) self.lineEdit = QtGui.QLineEdit(self.centralwidget) self.lineEdit.setGeometry(QtCore.QRect(220, 390, 351, 31)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(14) font.setBold(True) font.setWeight(75) self.lineEdit.setFocus(True) self.lineEdit.setFont(font) self.lineEdit.setObjectName(_fromUtf8("lineEdit")) self.lineEdit.returnPressed.connect(self.txt_to_lbl) self.label = QtGui.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(570, 30, 101, 16)) self.label.setTextFormat(QtCore.Qt.AutoText) self.label.setObjectName(_fromUtf8("label")) self.label_2 = QtGui.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(570, 60, 101, 16)) self.label_2.setObjectName(_fromUtf8("label_2")) self.label_3 = QtGui.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(570, 90, 101, 16)) self.label_3.setObjectName(_fromUtf8("label_3")) self.label_4 = QtGui.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(570, 120, 101, 21)) self.label_4.setObjectName(_fromUtf8("label_4")) self.label_5 = QtGui.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(570, 150, 101, 16)) self.label_5.setObjectName(_fromUtf8("label_5")) self.label_6 = QtGui.QLabel(self.centralwidget) self.label_6.setGeometry(QtCore.QRect(570, 180, 101, 16)) self.label_6.setObjectName(_fromUtf8("label_6")) self.label_7 = QtGui.QLabel(self.centralwidget) self.label_7.setGeometry(QtCore.QRect(570, 210, 101, 16)) self.label_7.setObjectName(_fromUtf8("label_7")) self.label_8 = QtGui.QLabel(self.centralwidget) self.label_8.setGeometry(QtCore.QRect(570, 240, 101, 16)) self.label_8.setFrameShape(QtGui.QFrame.NoFrame) self.label_8.setFrameShadow(QtGui.QFrame.Sunken) self.label_8.setObjectName(_fromUtf8("label_8")) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtGui.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21)) self.menubar.setObjectName(_fromUtf8("menubar")) self.menuFile = QtGui.QMenu(self.menubar) self.menuFile.setObjectName(_fromUtf8("menuFile")) self.menuAbout = QtGui.QMenu(self.menubar) self.menuAbout.setObjectName(_fromUtf8("menuAbout")) MainWindow.setMenuBar(self.menubar) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName(_fromUtf8("statusbar")) MainWindow.setStatusBar(self.statusbar) self.actionClose = QtGui.QAction(MainWindow) self.actionClose.setObjectName(_fromUtf8("actionClose")) self.actionAbout = QtGui.QAction(MainWindow) self.actionAbout.setObjectName(_fromUtf8("actionAbout")) self.menuFile.addAction(self.actionClose) self.menuAbout.addAction(self.actionAbout) self.menubar.addAction(self.menuFile.menuAction()) self.menubar.addAction(self.menuAbout.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "MC Post scanner", None)) self.btnToExcel.setToolTip(_translate("MainWindow", "Export your file to MS Excel", None)) self.btnToExcel.setText(_translate("MainWindow", "Export to Excel", None)) self.btnBack.setToolTip(_translate("MainWindow", "If you made a mistake, press for 1 step back", None)) self.btnBack.setText(_translate("MainWindow", "Back", None)) self.checkBox.setText(_translate("MainWindow", " Scan nu het trackingsnummer", None)) self.checkBox_2.setText(_translate("MainWindow", " Aantal pakketten", None)) self.checkBox_3.setText(_translate("MainWindow", " Welk postbedrijf?", None)) self.checkBox_4.setText(_translate("MainWindow", " Lichtgrijs", None)) self.checkBox_5.setText(_translate("MainWindow", " CheckBox", None)) self.checkBox_6.setText(_translate("MainWindow", " CheckBox", None)) self.checkBox_7.setText(_translate("MainWindow", " CheckBox", None)) self.checkBox_8.setText(_translate("MainWindow", " CheckBox", None)) self.label.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_2.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_3.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_4.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_5.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_6.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_7.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_8.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.menuFile.setTitle(_translate("MainWindow", "File", None)) self.menuAbout.setTitle(_translate("MainWindow", "Help", None)) self.actionClose.setText(_translate("MainWindow", "Close", None)) self.actionClose.setShortcut(_translate("MainWindow", "Ctrl+Q", None)) self.actionAbout.setText(_translate("MainWindow", "About", None)) def txt_to_lbl(self): txt = self.lineEdit.text() self.label.setText(txt) self.lineEdit.selectAll() if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) MainWindow = QtGui.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
Отредактировано Puten (Фев. 15, 2016 23:24:47)
Прикреплённый файлы:
hz22.py (11,9 KБ)
Офлайн
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys try: from PyQt4 import QtGui except ImportError: from PyQt5 import QtWidgets as QtGui class Widget(QtGui.QWidget): def __init__(self): super().__init__() self.counter = 0 self.label = [] self.line_edit = QtGui.QLineEdit() self.line_edit.returnPressed.connect(self.set_text) self.box = QtGui.QVBoxLayout(self) self.box.addWidget(self.line_edit) def add_labels(self, number_of_label): for i in range(number_of_label): self.label.append(QtGui.QLabel()) self.box.addWidget(self.label[i]) self.label[i].setNum(i) def set_text(self): if self.counter < len(self.label): self.label[self.counter].clear() self.label[self.counter].setText(self.line_edit.text()) self.line_edit.clear() self.counter += 1 else: print('некуда писать') if __name__ == '__main__': app = QtGui.QApplication(sys.argv) main = Widget() main.add_labels(8) main.show() sys.exit(app.exec_())
Офлайн
vrabeyДа, поняли правильно и ваш пример работает так, как надо. Посмотрю, как его встроить в мой. Спасибо!
если я правильно понял вопрос
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'hz2.ui' # # Created by: PyQt4 UI code generator 4.11.4 # # WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(800, 600) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth()) MainWindow.setSizePolicy(sizePolicy) MainWindow.setMinimumSize(QtCore.QSize(800, 600)) MainWindow.setMaximumSize(QtCore.QSize(800, 600)) MainWindow.setMouseTracking(False) MainWindow.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap(_fromUtf8("mc.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off) MainWindow.setWindowIcon(icon) MainWindow.setDocumentMode(False) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) # Строка ввода self.lineEdit = QtGui.QLineEdit(self.centralwidget) self.lineEdit.setGeometry(QtCore.QRect(220, 390, 351, 31)) font = QtGui.QFont() font.setFamily(_fromUtf8("Verdana")) font.setPointSize(14) font.setBold(True) font.setWeight(75) self.lineEdit.setFocus(True) self.lineEdit.setFont(font) self.lineEdit.setObjectName(_fromUtf8("lineEdit")) # Нажатие Enter self.lineEdit.returnPressed.connect(self.txt_to_lbl) # Лейблы self.label = QtGui.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(570, 30, 101, 16)) self.label.setTextFormat(QtCore.Qt.AutoText) self.label.setObjectName(_fromUtf8("label")) self.label_2 = QtGui.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(570, 60, 101, 16)) self.label_2.setObjectName(_fromUtf8("label_2")) self.label_3 = QtGui.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(570, 90, 101, 16)) self.label_3.setObjectName(_fromUtf8("label_3")) self.label_4 = QtGui.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(570, 120, 101, 21)) self.label_4.setObjectName(_fromUtf8("label_4")) self.label_5 = QtGui.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(570, 150, 101, 16)) self.label_5.setObjectName(_fromUtf8("label_5")) self.label_6 = QtGui.QLabel(self.centralwidget) self.label_6.setGeometry(QtCore.QRect(570, 180, 101, 16)) self.label_6.setObjectName(_fromUtf8("label_6")) self.label_7 = QtGui.QLabel(self.centralwidget) self.label_7.setGeometry(QtCore.QRect(570, 210, 101, 16)) self.label_7.setObjectName(_fromUtf8("label_7")) self.label_8 = QtGui.QLabel(self.centralwidget) self.label_8.setGeometry(QtCore.QRect(570, 240, 101, 16)) self.label_8.setFrameShape(QtGui.QFrame.NoFrame) self.label_8.setFrameShadow(QtGui.QFrame.Sunken) self.label_8.setObjectName(_fromUtf8("label_8")) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtGui.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21)) self.menubar.setObjectName(_fromUtf8("menubar")) MainWindow.setMenuBar(self.menubar) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName(_fromUtf8("statusbar")) MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "MC Post scanner", None)) self.label.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_2.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_3.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_4.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_5.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_6.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_7.setText(_translate("MainWindow", "Nog in te vullen...", None)) self.label_8.setText(_translate("MainWindow", "Nog in te vullen...", None)) def txt_to_lbl(self, txt=None): txt = self.lineEdit.text() self.label.setText(txt) self.lineEdit.selectAll() if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) MainWindow = QtGui.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
Отредактировано Puten (Фев. 16, 2016 12:18:13)
Офлайн
Puten
Не получается встроить ваш пример
import sys try: from PyQt4 import QtGui except ImportError: from PyQt5 import QtWidgets as QtGui class LabelsBox(QtGui.QFrame): def __init__(self, label_lst): """ box с надписями :param label_lst: [str] список имён надписей """ super().__init__() box = QtGui.QVBoxLayout(self) self.label = [] for num, name in enumerate(label_lst): self.label.append(QtGui.QLabel(name)) box.addWidget(self.label[num]) class Widget(QtGui.QWidget): def __init__(self): super().__init__() self.counter = 0 self.setFixedSize(800, 600) self.labels_list_name = ['1_label', '2_label', '3_label', '4_label'] self.line_edit = QtGui.QLineEdit(self) self.line_edit.setGeometry(220, 390, 351, 31) self.line_edit.returnPressed.connect(self.add_text) # контейнер для boxa с надписями self.box_for_labels_group = QtGui.QFrame(self) self.box = QtGui.QHBoxLayout(self.box_for_labels_group) self.box_for_labels_group.setStyleSheet("background-color: white") self.box_for_labels_group.setGeometry(570, 30, 101, 360) # создаём box self.label_box = LabelsBox(self.labels_list_name) # и ложим в контейнер self.box.addWidget(self.label_box) def add_text(self): self.label_box.label[self.counter].clear() self.label_box.label[self.counter].setText(self.line_edit.text()) self.line_edit.clear() self.counter += 1 if __name__ == '__main__': app = QtGui.QApplication(sys.argv) main = Widget() main.show() sys.exit(app.exec_())
Офлайн
vrabeyАга, пример работает правильно.
я бы сделал так, но не факт что это ‘как надо’.
Офлайн