[python] Ejemplo de implementación de código orientado a objetos PySide

from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit,QMessageBox

class first_example:
    def __init__(self):
        self.window = QMainWindow()
        self.window.move(300,180) #窗口位置
        self.window.resize(400,400) #大小

        self.text = QPlainTextEdit(self.window)
        self.text.move(10,30)
        self.text.resize(180,300)

        self.button = QPushButton('统计',self.window)
        self.button.move(250,50)
        self.button.resize(30,30)
        self.button.clicked.connect(self.calculate) #button回调函数,术语也叫为signal,所回调的函数叫slot

    def calculate(self):
        info = self.text.toPlainText() #获取对象text中的字符文本
        over_20000 = ""
        below_20000 = ""
        for line in info.splitlines(): #获取每行
            if not line.strip():#记得加判断是否为空
                continue
            parts = line.split(' ')
            parts = [p for p in parts if p]
            name,salary,age = parts
            if int(salary) <= 20000:
                below_20000 += name + '\n'
            else:
                over_20000 += name + '\n'

        QMessageBox.about(self.window,'统计结果',f'薪资20000以下的有:\n{below_20000}\n\n薪资20000以上的有:\n{over_20000}') #弹出消息窗口

app = QApplication([])
example = first_example()
example.window.show()
app.exec_()

Inserte la descripción de la imagen aquí

from Main import get_result
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader

class Stats:
    def __init__(self):
        self.ui = QUiLoader().load('windows.ui')
        self.ui.button.clicked.connect(self.return_info)

    def return_info(self):
        username = self.ui.number_input.toPlainText()
        userpassword = self.ui.password_input.toPlainText()
        a = get_result(username, userpassword)
        self.ui.info_window.setText(str(a))

app = QApplication([])
main = Stats()
main.ui.show()
app.exec_()

Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/Sgmple/article/details/111874210
Recomendado
Clasificación