伙伴关系
:设置两个控件之间的关联,通过一个控件控制另一个控件。
1、首先选择在Widget Box中拖入一个Frame
属性放在窗口中,再往窗口中添加Label
、Line Edit
属性,并且修改Label
属性的显示值,在这里我采用容器布局管理,对整个Frame
容器使用表单布局,效果图如下:
2、向每个Label属性添加热键(&A)、(&B)、(&C),如下图:
热键
:又叫快捷键
,通过操控键盘的特点键来快速完成指定的操作,代替了鼠标的繁琐移动。如:Ctrl+A,Ctrl+C,Alt、Shift等等。
3、然后点击菜单栏中的Edit
选项,选择Edit Buddies
来设置伙伴关系,鼠标左键按住Label
,向右拖到Line Edit
,这样伙伴关系就设置好了。如下图:
4、按Esc键退出,Ctrl+R预览效果,按Alt+A光标就可以跳转到姓名后面的编辑栏,Alt+B和Alt+C同理。转换成.py文件的代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'D:\Python--PyQt5--Eric6\first.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(160, 130, 232, 101))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.formLayout = QtWidgets.QFormLayout(self.frame)
self.formLayout.setObjectName("formLayout")
self.label = QtWidgets.QLabel(self.frame)
self.label.setObjectName("label")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setObjectName("lineEdit")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setObjectName("label_2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setObjectName("lineEdit_2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit_2)
self.label_3 = QtWidgets.QLabel(self.frame)
self.label_3.setObjectName("label_3")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
self.lineEdit_3 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_3.setObjectName("lineEdit_3")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.lineEdit_3)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.label.setBuddy(self.lineEdit)
self.label_2.setBuddy(self.lineEdit_2)
self.label_3.setBuddy(self.lineEdit_3)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "姓名(&A)"))
self.label_2.setText(_translate("MainWindow", "年龄(&B)"))
self.label_3.setText(_translate("MainWindow", "性别(&C)"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())