简体   繁体   中英

By clicking a button, print some texts entered in QLineEdit when a checkbox is checked PyQt4

By clicking a button, I want to print some texts that is entered in QLineEdit when a checkbox is checked. My example codes are as below:

import sys
import PyQt4
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Widget(QWidget):
    def __init__(self, parent= None):
        super(Widget, self).__init__(parent)
        layout = QGridLayout()

        self.setLayout(layout)

        self.checkBox = QCheckBox()
        layout.addWidget(self.checkBox, 0, 0)


        self.le = QLineEdit()
        layout.addWidget(self.le, 0, 1)

        self.btn = QPushButton('Run')
        layout.addWidget(self.btn, 0, 3)


class Func ():
    def __init__(self):
        a = Widget(self)

    def someFunc(self):
        ##print ()


app = QApplication(sys.argv)
widget = Widget()
widget.show()
app.exec_()

As you can see above, I want the button in "Widget" class to connect to "someFunc" method in "Func" class. Thus when some texts are entered in "self.le" as well as "checkBox" is checked, I want "someFunc" to print the texts entered in "self.le" by clicking the button. If the "checkbox" is not checked, clicking the button should not cause anything to happen even when some texts are entered.

If anyone knows how to solve it, pls let me know thanks!!

You need to connect the button's clicked signal to the function that will handle it. Like this: button.clicked.connect(handler_function)

import sys
import PyQt5
from PyQt5.QtWidgets import *

class Func ():
    def __init__(self, widget):
        self.w = widget

    def someFunc(self):
        if self.w.checkBox.isChecked():
            print(self.w.le.text())

class Widget(QWidget):
    def __init__(self, parent= None):
        super(Widget, self).__init__(parent)
        layout = QGridLayout()

        self.setLayout(layout)

        self.checkBox = QCheckBox()
        layout.addWidget(self.checkBox, 0, 0)


        self.le = QLineEdit()
        layout.addWidget(self.le, 0, 1)

        self.btn = QPushButton('Run')
        layout.addWidget(self.btn, 0, 3)

        # connecting to a method in this class
        # self.btn.clicked.connect(self.some_func)

        #connecting to a method in another class
        self.handler_class = Func(self)
        self.btn.clicked.connect(self.handler_class.someFunc)

    def some_func(self):
        if self.checkBox.isChecked():
            print(self.le.text())


app = QApplication(sys.argv)
widget = Widget()
widget.show()
app.exec_()

Edit: Put simply: In the Func class self.w contains a reference to the widget from which a signal will be emitted when the button is clicked.

Why am I keeping a reference to that widget? So that I can access the widget's combobox and lineedit . Without a way to access them I can't see if the checkbox is checked or what the user typed in the textedit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM