首页 > 解决方案 > how to Activate QLineEdit when QCheckBox pyqt5 qt designer is checked?

问题描述

I just started learning pyqt5 qt designer. I used to use tkinter. I have a problem, and I can't figure out how to do it. As the picture shows, there are QCheckBox and QLineEdit. Keep the LineEdit field 'disabled' when checkbox is not checked; When the CheckBox is checked, I try to get the LineEdit field to be activated in the 'normal' position.

enter image description here

enter image description here

when i use tkinter,

txtSample.configure(state=DISABLED)

txtSample = Checkbutton( text='TEST', variable=var, onvalue=1, offvalue=0,font('arial',16,'bold'))

txtSample = Entry(font=('arial',16,'bold'), bd=8, width=6, justify='left',state=DISABLED)

def checkbuttonSample():
    if(var.get() == 1):
        txtSample.configure(state=NORMAL)
    elif var.get()== 0:
        txtSample.configure(state=DISABLED)`

I would use similar code like this.But I couldn't find a solution and I am looking for a solution about this issue. Thank you for your answers in advance.

标签: pythonpyqt5qt-designer

解决方案


从代码来看非常简单:您需要将toggled复选框的信号连接到setEnabled()行编辑的插槽:

self.lineEdit.setEnabled(False)
self.checkBox.toggled.connect(self.lineEdit.setEnabled)

您也可以直接在 Designer 中执行此操作:

  • 选择行编辑;
  • 在“属性编辑器”面板上,取消选中“启用”选项;
  • 进入“编辑信号/槽”模式(从工具栏或“编辑”菜单);
  • 在复选框上按下鼠标左键,在行编辑上移动鼠标,然后松开按钮;
  • 在随后打开的“配置连接”对话框中,选择左侧的“toggled(bool)”信号,勾选“显示从QWidget继承的信号和插槽”选项,并选择“setEnabled(bool)”插槽,然后点击“确定”;

推荐阅读