我正在编写一个简单的应用程序,该应用程序将从用户那里获取一些输入然后继续进行。
我有两个文本框,要求输入用户名和姓氏。然后有两个文本框-一个用于Case1,第二个来自Case2。我想从用户那里获得一些其他信息。选择Case1或Case2后,这些将有所不同。
在按下Case1或Case2之后,我想在同一窗口中显示新的textBox小部件(QLineEdit)。在下面的代码中类似self.textbox_name = QLineEdit(self)
。
这是我的问题。选中这些复选框后是否可以显示不同的新文本框,然后单击添加后将其关闭?
这里有我的代码
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import * #Imported one more module
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class AddRecord(QMainWindow):
def __init__(self):
super().__init__()
self.title='Add a record'
self.left=10
self.top=10
self.width=640
self.height=480
self.initUI()
self.line_edit = QtWidgets.QLineEdit()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)
self.statusBar().showMessage('In progress')
etykieta1 = QLabel("Name: ", self)
etykieta1.setGeometry(QtCore.QRect(etykieta1.x(), etykieta1.y(), etykieta1.width()+50, etykieta1.height()))
etykieta1.move(5, 5)
self.textbox_name = QLineEdit(self)
self.textbox_name.move(150, 5)
self.textbox_name.resize(280,25)
etykieta2 = QLabel("Surname: ", self)
etykieta2.setGeometry(QtCore.QRect(etykieta1.x(), etykieta1.y(), etykieta1.width()+50, etykieta1.height()))
etykieta2.move(5, 35)
self.textbox_surname = QLineEdit(self)
self.textbox_surname.move(150, 40)
self.textbox_surname.resize(280,25)
button_back = QPushButton('Add a record',self)
button_back.clicked.connect(self.on_click)
button_back.resize(200,32)
button_back.move(0,425)
self.checkbox_case1 = QCheckBox("Case1",self)
self.checkbox_case1.stateChanged.connect(self.clickBox)
self.checkbox_case1.move(20,100)
self.checkbox_case1.resize(320,40)
self.checkbox_case2 = QCheckBox("Case2",self)
self.checkbox_case2.stateChanged.connect(self.clickBox)
self.checkbox_case2.move(130,100)
self.checkbox_case2.resize(320,40)
self.show()
@pyqtSlot()
def on_click(self):
textboxValue_name = self.textbox_name.text()
textboxValue_surname = self.textbox_surname.text()
QMessageBox.question(self, 'Warning', 'NewRecordAdded.', QMessageBox.Ok, QMessageBox.Ok)
self.textbox_name.setText("")
self.textbox_surname.setText("")
print("Works well", textboxValue_name, textboxValue_surname)
self.checkbox_case1.setChecked(False)
self.checkbox_case2.setChecked(False)
def clickBox(self, state):
if state == QtCore.Qt.Checked:
print('Checked')
else:
print('Unchecked')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = AddRecord()
sys.exit(app.exec_())
答案 0 :(得分:0)
我对您的逻辑有以下看法:
使用布局设置小部件的位置。
如果要处理许多按钮,例如QCheckBox,建议使用QButtonGroup。
要解决此问题,您必须视情况使用show(),hide()或setVisible()方法显示或隐藏小部件。
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()
def initUI(self):
self.setWindowTitle("Add a record")
self.statusBar().showMessage("In progress")
self.name_edit = QtWidgets.QLineEdit()
self.surname_edit = QtWidgets.QLineEdit()
self.case1_checkbox = QtWidgets.QCheckBox("Case1")
self.case2_checkbox = QtWidgets.QCheckBox("Case2")
self.m_group = QtWidgets.QButtonGroup(
self, exclusive=False, buttonClicked=self.onButtonClicked
)
self.m_group.addButton(self.case1_checkbox)
self.m_group.addButton(self.case2_checkbox)
# hide textbox
self.textbox_name = QtWidgets.QLineEdit()
self.textbox_name.hide()
self.add_button = QtWidgets.QPushButton(
"Add a record", clicked=self.on_click
)
self.add_button.setFixedSize(200, 32)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
flay = QtWidgets.QFormLayout()
flay.addRow("Name", self.name_edit)
flay.addRow("Surname", self.surname_edit)
hlay = QtWidgets.QHBoxLayout()
hlay.addWidget(self.case1_checkbox)
hlay.addWidget(self.case2_checkbox)
hlay.addStretch()
vlay = QtWidgets.QVBoxLayout(central_widget)
vlay.addLayout(flay)
vlay.addLayout(hlay)
vlay.addWidget(self.textbox_name)
vlay.addStretch()
vlay.addWidget(self.add_button, alignment=QtCore.Qt.AlignLeft)
self.resize(640, 480)
@QtCore.pyqtSlot()
def onButtonClicked(self):
self.textbox_name.setVisible(self.m_group.checkedId() != -1)
@QtCore.pyqtSlot()
def on_click(self):
QtWidgets.QMessageBox.question(
self,
"Warning",
"NewRecordAdded.",
QtWidgets.QMessageBox.Ok,
QtWidgets.QMessageBox.Ok,
)
# restore
self.name_edit.clear()
self.surname_edit.clear()
self.textbox_name.clear()
self.textbox_name.hide()
for button in self.m_group.buttons():
button.setChecked(False)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
如果您检查代码,则未添加任何按钮,仅使用了小部件的可见性。