Python+QT开发碰到的问题

1 篇文章 0 订阅

1.定义按钮点击函数,但是在运行时函数直接启动,之后点击按钮无反应

函数定义如下:

def handleCalc():
    info = textEdit.toPlainText()

    # 薪资20000 以上 和 以下 的人员名单
    salary_above_20k = ''
    salary_below_20k = ''
    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:
            salary_above_20k += name + '\n'
        else:
            salary_below_20k += name + '\n'
    QMessageBox.about(window,
                      '统计结果',
                      f'''薪资20000 以上的有:\n{salary_above_20k}
                    \n薪资20000 以下的有:\n{salary_below_20k}'''
                      )

调用如下:

button = QPushButton('统计', window)
button.move(380,80)
button.clicked.connect(handleCalc())

这时启动程序会直接运行QMessageBox

 问题在于函数绑定这里,应该改成

button.clicked.connect(handleCalc)

这时启动就正常了,原理暂未搞清,待深入之后补充


2.找不到PySide2模块

Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit, QMessageBox, QLineEdit,QVBoxLayout
ModuleNotFoundError: No module named 'PySide2'

开头已经导入了模块,但是总是提示我出错,查看了一下发现,因为我把Python文件命名为“os.py”,导致和相关文件冲突,文件改名后就正常了。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用PythonQt实现记事本的基本步骤: 1.安装PyQt库 PyQt是一个Python库,用于创建GUI应用程序。它可以在PyPI上通过pip包管理器安装。 2.创建一个Qt窗口应用程序 使用Qt Designer创建一个窗口,添加文本编辑器和菜单栏等必要的组件。 3.在Python中加载Qt窗口 使用PyQt加载Qt窗口,将窗口上的组件与Python代码连接。 4.添加文件读写功能 为了让用户能够打开和保存文件,需要添加文件读写功能。 5.添加其他功能 根据需求,可以添加其他功能,如剪切、复制和粘贴等。 以下是Python代码的示例: import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QFileDialog class Notepad(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.textEdit = QTextEdit() self.setCentralWidget(self.textEdit) self.statusBar() menubar = self.menuBar() fileMenu = menubar.addMenu('File') openFile = QAction('Open', self) openFile.setShortcut('Ctrl+O') openFile.triggered.connect(self.showDialog) saveFile = QAction('Save', self) saveFile.setShortcut('Ctrl+S') saveFile.triggered.connect(self.saveDialog) fileMenu.addAction(openFile) fileMenu.addAction(saveFile) self.setGeometry(300, 300, 350, 300) self.setWindowTitle('Notepad') self.show() def showDialog(self): fname = QFileDialog.getOpenFileName(self, 'Open file', '/home') if fname[0]: f = open(fname[0], 'r') with f: data = f.read() self.textEdit.setText(data) def saveDialog(self): fname = QFileDialog.getSaveFileName(self, 'Save file', '/home') if fname[0]: f = open(fname[0], 'w') with f: f.write(self.textEdit.toPlainText()) if __name__ == '__main__': app = QApplication(sys.argv) ex = Notepad() sys.exit(app.exec_())

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值