【Pyside6】桌面应用--目录、代码结构设计(附案例)


前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。 点击跳转到网站。

目录设计的必要性

1、每个目录层次有自己的独立的职责,具备不同的功能,协同合作成就一个项目。
2、随着功能模块的增多、代码的逻辑的复杂性增强,模块之间的耦合性问题逐渐显露。良好的目录设计可以有效防止模块的混乱,也能做好对功能模块的快速定位。这对于开发人员来讲是尤其重要的。
3、随着时间的推移,后期再回过头来对项目进行维护时,清晰的目录结构可以让我们快速理清思路、关键定位。

代码结构设计的必要性

1、对于长期的维护,代码设计比可不少。他决定了代码整体开发风格和编写形式。
2、增加可读性、可维护性。松耦合、划分层次。
3、使功能模块更容易理解,代码质量、扩展性更好。

案例(以登录、注册、主界面为例。)

项目演示

Pyside6-登录、注册界面

目录结构分析

在这里插入图片描述

conf:配置一些三方参数:如接口请求地址、路径、数据库信息等
img:存放引用图片(通过接口从后台获取图片也是可行的)
qss:存放界面样式文件
src:为项目源码文件。每个界面为一个module。module中包含原始ui文件和逻辑代码文件
ui:本例采用designer设计ui,故将项目所有ui文件,存于ui文件中
app.py:文件为项目启动文件

代码结构分析

app.py
main类将所有界面做集合,目的是将所有的界面组件联动。如图所示。self包含所有界面代码,将self作为实参传给每个模块类,实现界面联动。

class main():
    def __init__(self):
        super(main, self).__init__()

        '将所有的界面做集合'
        self.login_ui = Login(self)
        self.regis_ui = Regis(self)
        self.index_ui = Index(self)

        '初始化登录界面'
        self.login_ui.login_win.show()

login_module>login_ui
ui文件不多赘述,但注意:ui文件不宜手动修改,因为它是自动生成的,或许在开发阶段还需要ui源文件通过designer进行增删改的操作。此时再转换为py时,可以与逻辑文件兼容。

class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(373, 249)
        self.lineEdit = QLineEdit(Form)
        self.lineEdit.setObjectName(u"lineEdit")
        self.lineEdit.setGeometry(QRect(100, 70, 171, 21))
        self.lineEdit_2 = QLineEdit(Form)
        self.lineEdit_2.setObjectName(u"lineEdit_2")
        self.lineEdit_2.setGeometry(QRect(100, 110, 171, 21))
        self.pushButton = QPushButton(Form)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(110, 180, 161, 31))
        self.label = QLabel(Form)
        self.label.setObjectName(u"label")
        self.label.setGeometry(QRect(170, 20, 91, 31))
        self.label.setStyleSheet(u"font: 900 11pt \"Arial Black\";")
        self.checkBox = QCheckBox(Form)
        self.checkBox.setObjectName(u"checkBox")
        self.checkBox.setGeometry(QRect(110, 150, 80, 20))
        self.checkBox_2 = QCheckBox(Form)
        self.checkBox_2.setObjectName(u"checkBox_2")
        self.checkBox_2.setGeometry(QRect(200, 150, 80, 20))

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi


    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"the app", None))
        self.pushButton.setText(QCoreApplication.translate("Form", "登录", None))
        self.label.setText(QCoreApplication.translate("Form", "登录", None))
        self.checkBox.setText(QCoreApplication.translate("Form", "自动登录", None))
        self.checkBox_2.setText(QCoreApplication.translate("Form", "记住密码", None))
    # retranslateUi

login_module>login
继承ui类,此文件中编写逻辑代码。每个逻辑文件的主类中的init都将整个项目模块的代码作为实参传入,故项目每个模块都可以实现对全部模块的联动。

class MyQLabel(QLabel):
    # 自定义信号, 注意信号必须为类属性
    button_clicked_signal = Signal()

    def __init__(self, parent=None):
        super(MyQLabel, self).__init__(parent)

    def mouseReleaseEvent(self, QMouseEvent):
        self.button_clicked_signal.emit()

    # 可在外部与槽函数连接
    def connect_customized_slot(self, func):
        self.button_clicked_signal.connect(func)


class Login(Ui_Form):
    def __init__(self,main):
        super(Login, self).__init__()
        self.main=main
        self.login_win=QWidget()
        self.setupUi(self.login_win)

        '背景提示文字'
        self.lineEdit.setPlaceholderText("name")
        self.lineEdit_2.setPlaceholderText("password")
        '设置密码格式'
        self.lineEdit_2.setEchoMode(QLineEdit.Password)

        self.label2=MyQLabel(self.login_win)
        self.label2.setText('点击注册')
        self.label2.setGeometry(QRect(10, 220, 91, 31))
        'label点击事件'
        self.label2.connect_customized_slot(self.login_regis_show)

        '禁止窗口拉伸、禁用最大化按钮'
        self.login_win.setFixedSize(self.login_win.width(), self.login_win.height())

        '登录,进入主界面'
        self.pushButton.clicked.connect(self.login_index_show)

    def login_regis_show(self):
        self.login_win.close()
        self.main.regis_ui.regis_win.show()

    def login_index_show(self):
        self.login_win.close()
        self.main.index_ui.index_win.show()

完整源码地址

链接:https://pan.baidu.com/s/1vdoJvlYN1rBvP50kPUIc9w
提取码:6xp8

  • 13
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会振刀的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值