Курсор не реагирует на lineEdit и как вынести _pos из класса MyLabel в класс MyWin?
Собственно сам код -
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(691, 385)
Form.setMinimumSize(QtCore.QSize(691, 385))
Form.setMaximumSize(QtCore.QSize(691, 385))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/ico/Python.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(0, 0, 701, 391))
self.label.setStyleSheet("border-image: url(:/img/Снимок.PNG);") #<--- Ваша картинка
self.label.setText("")
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(450, 274, 241, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(450, 295, 241, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(450, 316, 241, 21))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(Form)
self.lineEdit_4.setGeometry(QtCore.QRect(450, 337, 241, 21))
self.lineEdit_4.setObjectName("lineEdit_4")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(470, 360, 75, 23))
self.pushButton.setStyleSheet("QPushButton {\n"
" background-color: #54e346;\n"
" }\n"
" QPushButton:hover {\n"
" background-color: white;\n"
" }\n"
" QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
" }")
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(600, 360, 75, 23))
self.pushButton_2.setStyleSheet("QPushButton {\n"
" background-color: #fa7f72;\n"
" }\n"
" QPushButton:hover {\n"
" background-color: white;\n"
" }\n"
" QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
" }")
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.lineEdit.setPlaceholderText(_translate("Form", "Введите ip"))
self.lineEdit_2.setPlaceholderText(_translate("Form", "Введите организацию"))
self.lineEdit_3.setPlaceholderText(_translate("Form", "Введите улицу"))
self.lineEdit_4.setPlaceholderText(_translate("Form", "Введите город"))
self.pushButton.setText(_translate("Form", "Ok"))
self.pushButton_2.setText(_translate("Form", "Cancel"))
#import res_rc
import sys
from add_label_des import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class MyLabel(QtWidgets.QLabel):
def __init__(self, parent):
super().__init__()
self.parent = parent
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.parent.flag:
self._pos = event.pos() # <--- Взять
print(f'def mouseReleaseEvent(self, event): {event.pos()} <----')
self.parent.label_pos.setText(
f"<b style='color: #fff'>.</b> x:{self._pos.x()}, y:{self._pos.y()-10}")
self.parent.label_pos.move(self._pos.x(), self._pos.y()-10)
self.parent.label_pos.adjustSize()
self.parent.flag = False
class MyWin(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.flag = False
self.Mylabel = MyLabel(self)
self.pushMyButton = QtWidgets.QPushButton('Add', self.Mylabel)
self.pushMyButton.move(10, 10)
self.pushMyButton.clicked.connect(self.func_connect)
self.label_pos = QLabel(self.Mylabel)
grid = QtWidgets.QGridLayout(self)
grid.addWidget(self.Mylabel)
self.pushButton.clicked.connect(self.add_new_label)
#self.pushButton_2.clicked.connect(self.add_new_row)
def func_connect(self):
self.flag = True
# self.pushButton.setEnabled(False)
def add_new_label(self):
text = self.lineEdit.text()
text2 = self.lineEdit_2.text()
text3 = self.lineEdit_3.text()
text4 = self.lineEdit_4.text()
CoordX = None # <----- Получить _pos.x()
CoordY = None # <----- Получить _pos.y()
if text and text2 and text3 and text4:
print(f"{text}\n{text2}\n{text3}\n{text4}")
else:
msg = QMessageBox.information(self, 'Внимание', 'Заполните все поля!')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MyWin()
w.show()
sys.exit(app.exec_())
И в идеале ещё избавиться от кнопки Add.
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Пробуйте так:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
#from add_label_des import *
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(691, 385)
Form.setMinimumSize(QtCore.QSize(691, 385))
Form.setMaximumSize(QtCore.QSize(691, 385))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/ico/Python.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
# !!! MyLabel
# self.label = QtWidgets.QLabel(Form)
self.label = MyLabel(Form) # !!! MyLabel
self.label.setGeometry(QtCore.QRect(0, 0, 701, 391))
self.label.setStyleSheet("border-image: url(im.png);") #<--- Ваша картинка
self.label.setText("")
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(450, 274, 241, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(450, 295, 241, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(450, 316, 241, 21))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(Form)
self.lineEdit_4.setGeometry(QtCore.QRect(450, 337, 241, 21))
self.lineEdit_4.setObjectName("lineEdit_4")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(470, 360, 75, 23))
self.pushButton.setStyleSheet("QPushButton {\n"
" background-color: #54e346;\n"
" }\n"
" QPushButton:hover {\n"
" background-color: white;\n"
" }\n"
" QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
" }")
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(600, 360, 75, 23))
self.pushButton_2.setStyleSheet("QPushButton {\n"
" background-color: #fa7f72;\n"
" }\n"
" QPushButton:hover {\n"
" background-color: white;\n"
" }\n"
" QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
" }")
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.lineEdit.setPlaceholderText(_translate("Form", "Введите ip"))
self.lineEdit_2.setPlaceholderText(_translate("Form", "Введите организацию"))
self.lineEdit_3.setPlaceholderText(_translate("Form", "Введите улицу"))
self.lineEdit_4.setPlaceholderText(_translate("Form", "Введите город"))
self.pushButton.setText(_translate("Form", "Ok"))
self.pushButton_2.setText(_translate("Form", "Cancel"))
class MyLabel(QtWidgets.QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.parent.flag:
self._pos = event.pos() # <--- Взять
print(f'def mouseReleaseEvent(self, event): {event.pos()} <----')
self.parent.label_pos.setText(
f"<b style='color: #fff'>.</b> x:{self._pos.x()}, y:{self._pos.y()-10}")
self.parent.label_pos.move(self._pos.x(), self._pos.y()-10)
self.parent.label_pos.adjustSize()
self.parent.flag = False
# !!! +++
self.parent.coordX = self._pos.x() # !!! +++
self.parent.coordY = self._pos.y()-10 # !!! +++ # ??? -10
class MyWin(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.flag = False
self.coordX = None # !!! +++
self.coordY = None # !!! +++
# self.myLabel = MyLabel(self)
# vvvv
self.pushMyButton = QtWidgets.QPushButton('Add', self) # - self.myLabel)
self.pushMyButton.move(10, 10)
self.pushMyButton.clicked.connect(self.func_connect)
# vvvv
self.label_pos = QLabel(self) # - (self.myLabel)
# grid = QtWidgets.QGridLayout(self) # -
# grid.addWidget(self.myLabel) # -
self.pushButton.clicked.connect(self.add_new_label)
#self.pushButton_2.clicked.connect(self.add_new_row)
def func_connect(self):
self.flag = True
# self.pushButton.setEnabled(False)
def add_new_label(self):
text = self.lineEdit.text()
text2 = self.lineEdit_2.text()
text3 = self.lineEdit_3.text()
text4 = self.lineEdit_4.text()
# vvvvvvvvvvv
print(f'coordX = {self.coordX}') # <----- Получить _pos.x()
print(f'coordY = {self.coordY}') # <----- Получить _pos.y()
# ^^^^^^^^^^^
# self.coordX = None
# self.coordY = None
if text and text2 and text3 and text4:
print(f"{text}\n{text2}\n{text3}\n{text4}")
else:
msg = QMessageBox.information(self, 'Внимание', 'Заполните все поля!')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MyWin()
w.show()
sys.exit(app.exec_())
