QLineEdit 的问题
Issues with QLineEdit
在这段代码中,我有两行编辑和中间的一个按钮。一开始,光标在编辑的第一行。我想写一个函数(switchLineEdit),它会在两者之间切换,这意味着一旦你在启动程序后点击按钮,它会将光标移动到第二行编辑(第二行编辑将被简单地点击)和副反之亦然。这是代码:
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QWidget
# ----------------------------------------------------
def switchLineEdit():
pass
# ----------------------------------------------------
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Line Edit")
window.setFixedWidth(280)
window.setFixedHeight(260)
window.move(100, 50)
window.setStyleSheet(
"background-color: rgb(208, 208, 208);"
)
lineEdit1 = QtWidgets.QLineEdit(window)
lineEdit1.setGeometry(40, 30, 200, 40)
lineEdit1.setStyleSheet(
"background-color: rgb(255, 255, 255);"
"font: 11pt \'Trebuchet MS\';"
"border: 0px solid;"
"border-radius: 20px;"
"padding-left: 7px;"
"padding-right: 7px;"
)
lineEdit1.setMaxLength(30)
button = QtWidgets.QPushButton(window)
button.setGeometry(110, 100, 60, 60)
button.setStyleSheet(
"border: 0px solid;"
"background-color: rgb(24, 92, 36);"
"border-radius: 30px;"
)
lineEdit2 = QtWidgets.QLineEdit(window)
lineEdit2.setGeometry(40, 190, 200, 40)
lineEdit2.setStyleSheet(
"background-color: rgb(255, 255, 255);"
"font: 11pt \'Trebuchet MS\';"
"border: 0px solid;"
"border-radius: 20px;"
"padding-left: 7px;"
"padding-right: 7px;"
)
lineEdit2.setMaxLength(30)
lineEdit1.returnPressed.connect(lambda: print(lineEdit1.text()))
button.clicked.connect(switchLineEdit)
window.show()
sys.exit(app.exec_())
谁能帮我写函数?
逻辑是使用 hasFocus()
验证 QLineEdit 是否具有焦点,并使用 setFocus()
将其设置为另一个焦点。 QPushButton 会增加复杂性,因为当它被点击时,它会获得焦点,因此很难识别谁获得了焦点,因此一个可能的解决方案是将 Qt::NoFocus 设置为 focusPolicy,这样它就不会获得焦点。
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLineEdit, QPushButton, QVBoxLayout, QWidget
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setAttribute(Qt.WA_StyledBackground)
self.setStyleSheet(
"""
QWidget{
background-color: rgb(208, 208, 208);
}
QLineEdit{
background-color: rgb(255, 255, 255);
font: 11pt 'Trebuchet MS';
border: 0px solid;
border-radius: 20px;
padding-left: 7px;
padding-right: 7px;
}
QPushButton{
border: 0px solid;
background-color: rgb(24, 92, 36);
border-radius: 30px;
}
"""
)
self.lineEdit1 = QLineEdit(maxLength=30)
self.lineEdit1.setFixedSize(200, 40)
self.lineEdit2 = QLineEdit(maxLength=30)
self.lineEdit2.setFixedSize(200, 40)
button = QPushButton(focusPolicy=Qt.NoFocus)
button.setFixedSize(60, 60)
lay = QVBoxLayout(self)
lay.addWidget(self.lineEdit1, alignment=Qt.AlignCenter)
lay.addWidget(button, alignment=Qt.AlignCenter)
lay.addWidget(self.lineEdit2, alignment=Qt.AlignCenter)
self.setFixedSize(280, 260)
button.clicked.connect(self.handle_clicked)
def handle_clicked(self):
if self.lineEdit1.hasFocus():
self.lineEdit2.setFocus()
elif self.lineEdit2.hasFocus():
self.lineEdit1.setFocus()
def main():
app = QApplication(sys.argv)
window = Widget()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
在这段代码中,我有两行编辑和中间的一个按钮。一开始,光标在编辑的第一行。我想写一个函数(switchLineEdit),它会在两者之间切换,这意味着一旦你在启动程序后点击按钮,它会将光标移动到第二行编辑(第二行编辑将被简单地点击)和副反之亦然。这是代码:
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QWidget
# ----------------------------------------------------
def switchLineEdit():
pass
# ----------------------------------------------------
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Line Edit")
window.setFixedWidth(280)
window.setFixedHeight(260)
window.move(100, 50)
window.setStyleSheet(
"background-color: rgb(208, 208, 208);"
)
lineEdit1 = QtWidgets.QLineEdit(window)
lineEdit1.setGeometry(40, 30, 200, 40)
lineEdit1.setStyleSheet(
"background-color: rgb(255, 255, 255);"
"font: 11pt \'Trebuchet MS\';"
"border: 0px solid;"
"border-radius: 20px;"
"padding-left: 7px;"
"padding-right: 7px;"
)
lineEdit1.setMaxLength(30)
button = QtWidgets.QPushButton(window)
button.setGeometry(110, 100, 60, 60)
button.setStyleSheet(
"border: 0px solid;"
"background-color: rgb(24, 92, 36);"
"border-radius: 30px;"
)
lineEdit2 = QtWidgets.QLineEdit(window)
lineEdit2.setGeometry(40, 190, 200, 40)
lineEdit2.setStyleSheet(
"background-color: rgb(255, 255, 255);"
"font: 11pt \'Trebuchet MS\';"
"border: 0px solid;"
"border-radius: 20px;"
"padding-left: 7px;"
"padding-right: 7px;"
)
lineEdit2.setMaxLength(30)
lineEdit1.returnPressed.connect(lambda: print(lineEdit1.text()))
button.clicked.connect(switchLineEdit)
window.show()
sys.exit(app.exec_())
谁能帮我写函数?
逻辑是使用 hasFocus()
验证 QLineEdit 是否具有焦点,并使用 setFocus()
将其设置为另一个焦点。 QPushButton 会增加复杂性,因为当它被点击时,它会获得焦点,因此很难识别谁获得了焦点,因此一个可能的解决方案是将 Qt::NoFocus 设置为 focusPolicy,这样它就不会获得焦点。
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLineEdit, QPushButton, QVBoxLayout, QWidget
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setAttribute(Qt.WA_StyledBackground)
self.setStyleSheet(
"""
QWidget{
background-color: rgb(208, 208, 208);
}
QLineEdit{
background-color: rgb(255, 255, 255);
font: 11pt 'Trebuchet MS';
border: 0px solid;
border-radius: 20px;
padding-left: 7px;
padding-right: 7px;
}
QPushButton{
border: 0px solid;
background-color: rgb(24, 92, 36);
border-radius: 30px;
}
"""
)
self.lineEdit1 = QLineEdit(maxLength=30)
self.lineEdit1.setFixedSize(200, 40)
self.lineEdit2 = QLineEdit(maxLength=30)
self.lineEdit2.setFixedSize(200, 40)
button = QPushButton(focusPolicy=Qt.NoFocus)
button.setFixedSize(60, 60)
lay = QVBoxLayout(self)
lay.addWidget(self.lineEdit1, alignment=Qt.AlignCenter)
lay.addWidget(button, alignment=Qt.AlignCenter)
lay.addWidget(self.lineEdit2, alignment=Qt.AlignCenter)
self.setFixedSize(280, 260)
button.clicked.connect(self.handle_clicked)
def handle_clicked(self):
if self.lineEdit1.hasFocus():
self.lineEdit2.setFocus()
elif self.lineEdit2.hasFocus():
self.lineEdit1.setFocus()
def main():
app = QApplication(sys.argv)
window = Widget()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()