成功打包py为deb——手把手教程

该文章已生成可运行项目,

打包之前准备的就是ico图标文件、打包脚本build-deb.sh和你的py程序文件。

ico文件,用gimp做,导出为,选择ico格式,大小32。

build-deb.sh脚本,你可以直接拿去用,改改PACKAGE_NAME等等即可:

#!/bin/bash

# 定义包名
PACKAGE_NAME="calculation-trainer"

# 创建目录结构
mkdir -p $PACKAGE_NAME/DEBIAN
mkdir -p $PACKAGE_NAME/usr/local/bin
mkdir -p $PACKAGE_NAME/usr/local/share/icons/calculation-trainer
mkdir -p $PACKAGE_NAME/usr/share/applications

# 复制脚本到目录
cp calculation-trainer.py $PACKAGE_NAME/usr/local/bin/
chmod +x $PACKAGE_NAME/usr/local/bin/calculation-trainer.py

# 复制图标文件(修改为ico格式,使用gimp, 大小32)
cp app_icon.ico $PACKAGE_NAME/usr/local/share/icons/calculation-trainer/

cat << EOF > calculation-trainer.desktop
[Desktop Entry]
Name=算术训练器
Exec=/usr/bin/env python3 /usr/local/bin/calculation-trainer.py
Icon=/usr/local/share/icons/calculation-trainer/app_icon.ico
Terminal=false
Type=Application
Categories=Utility;
EOF

# 检查calculation-trainer.desktop文件是否存在
if [ -f "calculation-trainer.desktop" ]; then
    # 复制桌面入口文件,并更新图标路径为ico
    cp calculation-trainer.desktop $PACKAGE_NAME/usr/share/applications/
    sed -i 's|Icon=/usr/local/share/icons/calculation-trainer/app_icon.png|Icon=/usr/local/share/icons/calculation-trainer/app_icon.ico|' $PACKAGE_NAME/usr/share/applications/calculation-trainer.desktop
    chmod +x $PACKAGE_NAME/usr/share/applications/calculation-trainer.desktop
else
    echo "错误: calculation-trainer.desktop文件不存在,请先创建该文件。"
    exit 1
fi

# 创建控制文件
cat << EOF > $PACKAGE_NAME/DEBIAN/control
Package: $PACKAGE_NAME
Version: 1.3
Section: utils
Priority: optional
Architecture: all
Depends: python3
Maintainer: Your Name <your_email@example.com>
Description: 算术训练器
 This is a tool for doing exercises.
EOF

# 检查控制文件是否存在
if [ ! -f "$PACKAGE_NAME/DEBIAN/control" ]; then
    echo "错误: 控制文件 $PACKAGE_NAME/DEBIAN/control 未创建成功。"
    exit 1
fi

# 构建DEB包
fakeroot dpkg-deb --build $PACKAGE_NAME

# 检查DEB包是否生成
DEB_FILE="$PACKAGE_NAME.deb"
if [ ! -f "$DEB_FILE" ]; then
    echo "错误: DEB包 $DEB_FILE 未生成成功。"
    exit 1
fi

echo "DEB包已生成: $DEB_FILE"

# 修改DEB包和桌面入口文件的权限
chmod 777 "$DEB_FILE"
chmod 777 "calculation-trainer.desktop"

PY程序,要注意首行 #!/usr/bin/env python3    不可或缺!

#!/usr/bin/env python3
import sys
import os

try:    
    from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, \
         QVBoxLayout, QWidget, QLabel, QLineEdit
    from PySide6 import QtCore
    from PySide6.QtGui import QFont, QPalette, QKeySequence
except:
    os.system("pip3 install PySide6 --break-system-packages")
    from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, \
         QVBoxLayout, QWidget, QLabel, QLineEdit
    from PySide6 import QtCore
    from PySide6.QtGui import QFont, QPalette, QKeySequence

import random
import time


#path1 = os.getcwd().replace("\\", "/")
home_path = os.path.expanduser("~")
print(home_path)
try:
    os.makedirs(home_path +"/.calculation_trainer")
except:
    pass
path1 = home_path +"/.calculation_trainer"
print(path1)

today1 = time.strftime("%Y-%m-%d-%H-%M-%S")
print(today1)

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        global calc_str, calc, n, right_n, wrong_n, score, start_time, end_time, use_time, correct_rate
        
        n=0
        score=0
        right_n =0
        wrong_n =0
        correct_rate =1
        score =0

        start_time = time.time()

        x = random.randint(10, 50)
        y = random.randint(10, 50)
        add_minus_int = random.randint(0,1)
        if add_minus_int == 0 and x-y>=0:
            calc_str = "%d - %d" %(x, y)
            calc = x-y
        else:
            calc_str = "%d + %d" %(x, y)
            calc = x+y
            
        #print(calc_str, calc)

        self.setWindowTitle("加减法算术小程序QT版")
        self.resize(800, 400)
        self.status = self.statusBar()

        layout = QVBoxLayout()
        widget = QWidget(self)
        widget.setLayout(layout)
        widget.setGeometry(QtCore.QRect(10, 10, 780, 380))

        font = QFont("Arial", 100)

        self.main_label = QLabel(f"{calc_str}")
        self.main_label.setWordWrap(True)
        self.main_label.setFont(font)

        palette = QPalette()
        #palette.setColor(QPalette.Window, QtCore.Qt.yellow)
        palette.setColor(QPalette.WindowText, QtCore.Qt.red)
        self.main_label.setPalette(palette)
        
        self.main_label.setAlignment(QtCore.Qt.AlignCenter)
        layout.addWidget(self.main_label)

        self.entry1 = QLineEdit()
        self.entry1.setAlignment(QtCore.Qt.AlignCenter)
        self.entry1.setStyleSheet("background-color: lightblue; color: red")
        layout.addWidget(self.entry1)
        self.entry1.returnPressed.connect(self.calculate)
        self.entry1.setFont(QFont("Arial", 20))
##        self.entry1.setFixedWidth(300)
##        layout.setAlignment(self.entry1, QtCore.Qt.AlignCenter)

        self.btn1 = QPushButton("提交")
        self.btn1.clicked.connect(self.calculate)
        #self.btn1.setFixedWidth(300)
        layout.addWidget(self.btn1)
        #layout.setAlignment(self.btn1, QtCore.Qt.AlignCenter)
        
##        self.btn1.setShortcut("enter")  #绑定数字小键盘回车键
##        self.btn1.setShortcut("return")  #绑定主键盘回车键


        self.result_label = QLabel()
        self.result_label.setAlignment(QtCore.Qt.AlignCenter)

        self.result_label.setFixedHeight(30) #设置高度
        layout.addWidget(self.result_label)

        self.score_label = QLabel()
        self.score_label.setAlignment(QtCore.Qt.AlignCenter)

        self.score_label.setFixedHeight(30) #设置高度
        layout.addWidget(self.score_label)


        
  
    def calculate(self):

        global calc_str, calc, n, right_n, wrong_n, score, start_time, end_time, use_time, correct_rate
        
        print(self.entry1.text())
        #print(self.entry1.hasFocus())
        #self.entry1.setText("OK")

        end_time = time.time()

        use_time = end_time-start_time
        #print(use_time)

        n+=1

        

        if self.entry1.text()!="":
            if int(self.entry1.text()) == calc:
                self.result_label.setText(str(calc) + ",正确!")
                right_n +=1

                if correct_rate >= ((n+score)/(10+n+right_n)) and use_time <= ((n/2+400)/(score+10)):       
                    score +=1

                else:
                    with open(path1+"/错题记录.txt", "a+") as ff:
                        ff.write(today1 + ", 超时," + calc_str + ", 用时:" + "{:.2f}".format(use_time))
                        ff.write("\n")
                
            else:
                self.result_label.setText("错误! 正确答案是:" +str(calc))
                wrong_n +=1
                
                score -=1

                with open(path1+"/错题记录.txt", "a+") as ff:
                    ff.write(today1 + ", 错题," + calc_str +", 错误答案:" + self.entry1.text())
                    ff.write("\n")

            #刷新
            x = random.randint(10, 50)
            y = random.randint(10, 50)
            add_minus_int = random.randint(0,1)
            if add_minus_int == 0 and x-y>=0:
                calc_str = "%d - %d" %(x, y)
                calc = x-y
            else:
                calc_str = "%d + %d" %(x, y)
                calc = x+y
                
            self.main_label.setText(f"{calc_str}")
            self.entry1.setText("")
            
            correct_rate = right_n/n

            correct_rate_formatted = "{:.2f}".format(correct_rate)
            score_crit_formatted = "{:.2f}".format((n+score)/(10+n+right_n))
            use_time_formatted = "{:.2f}".format(use_time)
            use_time_score_crit_formatted = "{:.2f}".format((n/2+400)/(score+10))

            self.score_label.setText(f"做题数:{n},【  得分:{score}  】,正确率:{correct_rate_formatted}, 得分标准:{score_crit_formatted},用时:{use_time_formatted},得分标准:{use_time_score_crit_formatted}")
            
            start_time = time.time()

            with open(path1+"/"+today1+"做题记录.txt", "a+") as ff:
                ff.write(f"做题数:{n},得分:{score},正确率:{correct_rate_formatted}, 得分标准:{score_crit_formatted},用时:{use_time_formatted},得分标准:{use_time_score_crit_formatted} \n")

        
        else:
            pass



if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

打包过程:

oliveryty@oliveryty:~$ ls
 测试插入图片              os
 加法算术小程序py-to-deb   Pictures
 Arduino                  "PlayOnLinux's virtual drives"
 Desktop                   Public
 Documents                 pyautogui_1.py
 Downloads                 py-caesar-3.py
 eclipse                   pycs1.py
 fengshen-caicai.py        PyQt5版_加法算术小程序.py
 gdm3.png                  python-qt-talk.py
 hello.java                py-to-deb
 hello.py                  snap
 HelloQT                   sys
 HelloQT2                  Templates
 Music                     Videos
oliveryty@oliveryty:~$ cd 加法算术小程序py-to-deb
oliveryty@oliveryty:~/加法算术小程序py-to-deb$ ls
app_icon.ico  build-deb.sh  calculation-trainer.py
oliveryty@oliveryty:~/加法算术小程序py-to-deb$ sh ./build-deb.sh
dpkg-deb: 正在 'calculation-trainer.deb' 中构建软件包 'calculation-trainer'。
DEB包已生成: calculation-trainer.deb
oliveryty@oliveryty:~/加法算术小程序py-to-deb$ sudo apt install ./calculation-trainer.deb
[sudo] oliveryty 的密码: 
对不起,请重试。
[sudo] oliveryty 的密码: 
注意,选中 'calculation-trainer' 而非 './calculation-trainer.deb'
将要安装:
  calculation-trainer

摘要:
  升级:0,安装:1,卸载:0,不升级:29
  下载大小:0 B / 3,636 B
  所需的空间:0 B / 55.8 GB 可用

获取:1 /home/oliveryty/加法算术小程序py-to-deb/calculation-trainer.deb calculation-trainer all 1.3 [3,636 B]
正在选中未选择的软件包 calculation-trainer。
(正在读取数据库 ... 系统当前共安装有 454292 个文件和目录。)
准备解压 .../calculation-trainer.deb  ...
正在解压 calculation-trainer (1.3) ...
正在设置 calculation-trainer (1.3) ...
正在处理用于 desktop-file-utils (0.27-2build1) 的触发器 ...
正在处理用于 gnome-menus (3.36.0-1.1ubuntu3) 的触发器 ...
Notice: 由于文件'/home/oliveryty/加法算术小程序py-to-deb/calculation-trainer.deb'无法被用户'_apt'访问,已脱离沙盒并提权为根用户来进行下载。 - pkgAcquire::Run (13: 权限不够)
oliveryty@oliveryty:~/加法算术小程序py-to-deb$ 

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值