Python box and remove elements

Code shows!

from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication,QLabel
from PyQt5.QtCore import Qt
import sys,sip

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.settings()

    def settings(self):
        self.a = QCheckBox('是否更新', self)
        self.a.move(20, 20)
        self.a.stateChanged.connect(self.printresult)
        
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('复选框')
        self.show()

    def printresult(self):
        print(self.a.text())
        print(self.a.checkState())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Simple.
Create a box with QCheckBox.
Then bind signal.

The first is the content of the print box, is 'whether to update'.
The second is whether or not to print on the hook, the hook is 2, not on the hook is 0.

From this we can determine whether the user has no hook.

Of course, there is a way, it is this:

from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication,QLabel
from PyQt5.QtCore import Qt
import sys,sip

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.settings()

    def settings(self):
        self.a = QCheckBox('是否更新', self)
        self.a.move(20, 20)
        self.a.toggle()
        self.a.stateChanged.connect(self.printresult)
        
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('复选框')
        self.show()

    def printresult(self):
        print(self.a.text())
        print(self.a.checkState())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

This is the default self.a.toggle hook.

Then we look at an example, there is also a new content element is deleted. I'll go first. Pyqt5 sip is in the library, use sip.delete to delete elements. del probably can, but I do not, it is not clear.

from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication,QPushButton
from PyQt5.QtCore import Qt
import sys,sip

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.settings()

    def settings(self):
        self.a = QCheckBox('是否更新', self)
        self.a.move(20, 20)
        self.a.stateChanged.connect(self.b)

        self.yes = QPushButton('我选好了!',self)
        self.yes.move(50,50)
        self.yes.clicked.connect(self.printresult)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('复选框')
        self.show()

    def b(self):
        self.number = self.a.checkState()


    def printresult(self):
        try:
            if self.number == 2:
                print('更新完毕!')
                sip.delete(self.a)
                sip.delete(self.yes)
            if self.number == 0:
                sip.delete(self.a)
                sip.delete(self.yes)
                print('下次更新')
        except:
            sip.delete(self.a)
            sip.delete(self.yes)
            print('下次更新')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

I believe we did all right.

Well, this is today's Gui knowledge, if you like, might spend five seconds, plus followers, a point like this. If you have doubts, you can ask in the comments area, partners can also add my QQ: 3,418,772,261 . In the QQ, I can provide a Q & A. So the next issue goodbye, bye!

Published 24 original articles · won praise 75 · views 30000 +

Guess you like

Origin blog.csdn.net/Persia_king/article/details/105267264