• PyQt界面


    打开designer在界面上放置了一些控件,保存界面为mainUi.ui,将mainwin.ui转换为Ui_mainwin.py

    pyuic5 -o  mainwin.ui  ui_mainwin.py

    ui_mainwin.py代码都是自动生成的,大家可以不需要动。

    在生成的文件中有一个Ui_MainWindow类

    新的py文件main.py,代码如下:

    import sys
    from PyQt5.QtWidgets import QApplication,QMainWindow,QFileDialog
    import ui_mainwin


    class MainCode(QMainWindow,ui_mainwin.Ui_MainWindow):
    def __init__(self):
    QMainWindow.__init__(self)
    mainUi.Ui_MainWindow.__init__(self)
    self.setupUi(self)
    self.btn_save.clicked.connect(self.on_save)
    self.btn_open.clicked.connect(self.on_open)

    def on_save(self):
    FullFileName,_=QFileDialog.getSaveFileName (self, '文件另存为', r'./','TXT (*.txt)')
    set_text=self.txt_view.toPlainText()
    with open(FullFileName,'wt') as f:
    print(set_text, file = f)

    def on_open(self):
    txtstr=""
    FullFileName, _ = QFileDialog.getOpenFileName(self, '打开', r'./', 'TXT (*.txt)')
    with open(FullFileName, 'rt') as f:
    lines=f.readlines()
    for line in lines:
    txtstr=txtstr+line
    self.txt_view.setText(txtstr)


    if __name__=='__main__':
    app=QApplication(sys.argv)
    md=MainCode()
    md.show()
    sys.exit(app.exec_())


     MainCode类又提供了一个容器,这个类继承自QMainWindow,mainUi.Ui_MainWindow,在这个类的构造函数中运行类父类的构造函数,并且把它自己作为参数产地给setupUi,并且添加了信号&槽信息

     self.btn_save.clicked.connect(self.on_save)

    self.btn_open.clicked.connect(self.on_open)

    这两个语句使得信号与槽函数进行手工绑定

  • 相关阅读:
    跳跃表原理
    查看Oracle操作历史的试图
    【概念】为什么有时全表扫描比通过索引扫描效率更高
    oracle驱动表以及如何确定驱动表
    SpringBoot学习(三)-----配置Bean
    leetcode 面试题58
    leetcode 1365 有多少小于当前数字的数字
    leetcode 1342 将数字变成 0 的操作次数
    leetcode1313 解压缩编码列表
    leetcode 1071 字符串的最大公因子
  • 原文地址:https://www.cnblogs.com/bongem/p/11768479.html
Copyright © 2020-2023  润新知