在这段代码中,我有两个行编辑和一个中间的按钮。在开始时,光标在第一行编辑。我想写一个函数(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_())有人能帮我写这个函数吗?
发布于 2021-08-22 03:10:50
逻辑是使用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()https://stackoverflow.com/questions/68878131
复制相似问题