当前位置:   article > 正文

关于tinker调用pyqt5之后部分程序无法打包成exe或即使打包后打开exe仍然无法调用图片的完全解决_tinker.exe

tinker.exe

请在程序最前面加上:

# -*- coding: utf-8 -*-

由于tinker的使用方法简单,但是无法显示图片,在使用我的方法(参:

https://blog.csdn.net/weixin_43390435/article/details/101158379
  • 1

)后,pyinstaller常规方法无法打包exe,或者即使程控,打开也会报错

打包问题解决(请结合第2个大标题下的内容看,否则你可能会走弯路)

只有输入以下命令才能打包成exe

pyinstaller -F main.py -p py_dir
  • 1

其中main.py是主py文件,不需要输入额外文件名,但是要保证打包文件在同一目录

(参:

https://blog.csdn.net/maixiaochai/article/details/90052333
  • 1

)不过,此时生成的exe虽然可以打开但是在使用图片时会报错如图
在这里插入图片描述

打包后调用图片依然报错解决

解决方法(参:

https://www.cnblogs.com/hester/p/11460121.html
  • 1


我使用的是第一种方法
新建b.py输入

# Fix qt import error
# Include this file before import PyQt5
import os
import sys
import logging
 
 
def _append_run_path():
    if getattr(sys, 'frozen', False):
        pathlist = []
 
        # If the application is run as a bundle, the pyInstaller bootloader
        # extends the sys module by a flag frozen=True and sets the app
        # path into variable _MEIPASS'.
        pathlist.append(sys._MEIPASS)
 
        # the application exe path
        _main_app_path = os.path.dirname(sys.executable)
        pathlist.append(_main_app_path)
 
        # append to system path enviroment
        os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)
 
    logging.error("current PATH: %s", os.environ['PATH'])
 
 
_append_run_path()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

生成的文件与主程序,副程序放在同一目录
主程序导入PyQt5相关库之前导入

imoort b.py
  • 1

保存修改,推出
此时胜利就在前方
保证三个.py文件都在同一目录
还是老方法打开命令行

pyinstaller -F main.py -p py_dir
  • 1

测试后,已经解决了图片显示问题,exe单独运行没有问题,但是在显示图片的时候会在控制台打印一些无关紧要的东西,不影响运行

后记

解决PyQt5模块的程序打开后无法正常返回tinker模块的问题

在程序最后加上os.folk()即可,经过测试只有这个可以用,虽然会提示一些错误信息:os modle not have attribuate 'folk'诸如此类,但是完美解决在打开图片后,程序被异常终止的问题

至此,tinker调用PyQt5使用资源及后引发的种种问题完全解决

待解决的小BUG

点击‘退出’时显示name 'exit''is not define
打开图片时显示module'os'has no attribuate 'folk'

补充图片证明方法可用性

tk主界面
tinker主界面

调用PyQt5显示收款码
在这里插入图片描述

今天家人过生日,Copy一个小程序加点料(turtle动态绘制哆啦A梦同时播放音乐)送给他

在这里插入图片描述
在这里插入图片描述
使用比上文更简单的打包方法(但是会让程序产生大量依赖文件,很占用空间)

安装pip install cxfreeze

cd到xxx.py目录执行
cxfreeze xxx.py --target-dir dist
xxx.py是要打包的py文件
dist是打包后生成的文件夹
如果用此方法打包
且程序中调用文件
需要在程序中调用文件的地方
改用相对路径(直接打文件名)
并在打包完成后
把相应资源文件(要对应写代码的时候调用的文件的名字)放入dist文件夹根目录中
否则会调用不了文件
而且不能操作dist文件夹中的文件
否则会有未知错误

用这个方法打包,就这样一个小程序占用80多M,打包上文的程序更是直接占用250多M,很不友好,但是方便,一些送给别人的小程序可以这样打包
源码(请忽略PYQT5的部分,本来想用之前的方法整合成1个EXE,后来觉得整合好了估计都要后天了,没意义,所以就做了一点PYQT的就放弃了)

哆啦的代码C自

https://www.2cto.com/kf/201806/753261.html
  • 1

还有画哆啦A梦的程序虽然是C来的但是他的注释不够明确,我多次测试后添加了一些注释,不过身体颜色问题还是没解决,如果要播放音乐请自行准备文件和更改代码

import threading
def play_music():
        import pygame
        import time
        import b
        filepath = r"勾指起誓.mp3";
        pygame.mixer.init()
        # 加载音乐
        pygame.mixer.music.load(filepath)
        pygame.mixer.music.play(start=0.0)
        #播放时长,没有此设置,音乐不会播放,会一次性加载完
        time.sleep(300)
        pygame.mixer.music.play(loops)

#下面是画哆啦的所有代码
def painting():
    import turtle as t
    def my_goto(x,y):
        t.penup()
        t.goto(x,y)
        t.pendown()
    def eyes():
        t.tracer(False)
        a=2.5
        for i in range(120):
            if 0 <=i<30 or 60<=i<90:
                a-=0.05
                t.lt(3)
                t.fd(a)
            else:
                a+=0.05
                t.lt(3)
                t.fd(a)
        t.tracer(True)

    t.title('哆啦A梦—祝你生日快乐')
    t.pensize(3)
    t.penup()
    t.circle(150,40)
    t.pendown()
    t.fillcolor("#00a0de")#头颜色
    t.begin_fill()
    t.circle(150,280)
    t.end_fill()

    t.fillcolor("#e70010")#项圈颜色
    t.begin_fill()
    t.seth(0)
    t.fd(200)
    t.circle(-5,90)
    t.fd(10)
    t.circle(-5,90)
    t.fd(207)
    t.circle(-5,90)
    t.fd(10)
    t.circle(-5,90)
    t.end_fill()

    #脸
    t.fd(183)
    t.fillcolor('#ffffff')#脸颜色
    t.begin_fill()
    t.lt(45)
    t.circle(120,100)
    t.seth(90)
    eyes()
    t.penup()
    t.seth(180)
    t.fd(60)
    t.pendown()
    t.seth(90)
    eyes()
    t.penup()
    t.seth(180)
    t.fd(60)
    t.pendown()
    t.seth(215)
    t.circle(120,100)
    t.end_fill()
    my_goto(-10,158)
    t.fillcolor('#e70010')#鼻子
    t.begin_fill()
    t.circle(20)
    t.end_fill()
    my_goto(5,148)
    t.seth(270)
    t.fd(100)
    t.seth(0)
    t.circle(120,50)
    t.seth(230)
    t.circle(-120,100)
    #胡须
    my_goto(-37,135)
    t.seth(165)
    t.fd(60)
    my_goto(-37,125)
    t.seth(180)
    t.fd(60)
    my_goto(-37,115)
    t.seth(193)
    t.fd(60)
    my_goto(37,135)
    t.seth(15)
    t.fd(60)
    my_goto(37,125)
    t.seth(0)
    t.fd(60)
    my_goto(37,115)
    t.seth(-13)
    t.fd(60)
    my_goto(0,0)
    t.seth(0)
    t.penup()
    t.circle(150,50)
    t.pendown()

    #身躯
    t.fillcolor('#ffffff')#肚兜和肚子
    t.begin_fill()
    t.seth(30)
    t.end_fill()

    t.fillcolor('#00a0de')#右边胳膊
    t.begin_fill()
    t.fd(40)
    t.seth(70)
    t.circle(-30,270)
    t.seth(230)
    t.fd(80)
    t.end_fill()

    t.seth(-89)
    t.circle(-1000,10)
    t.seth(180)
    t.fd(70)
    t.seth(90)
    t.circle(30,180)
    t.seth(180)
    t.fd(70)
    t.seth(100)
    t.circle(-1000,9)
    t.seth(240)
    t.fillcolor('#00a0de')#左边胳膊
    t.begin_fill()
    t.fd(40)
    t.circle(-30,250)
    t.seth(50)
    t.fd(70)
    t.end_fill()

    my_goto(103.74,-197.59)
    t.fillcolor('#ffffff')#右脚
    t.begin_fill()
    t.seth(0)
    t.fd(15)
    t.circle(-15,180)
    t.fd(90)
    t.circle(-15,180)
    t.fd(10)
    t.end_fill()

    my_goto(-96.26,-197.59)
    t.seth(180)
    t.fillcolor('#ffffff')#左脚
    t.begin_fill()
    t.fd(15)
    t.circle(15,180)
    t.fd(90)
    t.circle(15,180)
    t.fd(10)
    t.end_fill()

    my_goto(-103.42,15.09)
    t.seth(0)
    t.fd(38)
    t.seth(230)
    t.fillcolor('#ffffff')#肚子
    t.begin_fill()
    t.circle(90,260)
    t.end_fill()

    my_goto(5,-40)
    t.seth(0)
    t.fillcolor('#ffffff')#百宝袋
    t.begin_fill()
    t.fd(70)
    t.seth(-90)
    t.circle(-70,180)
    t.seth(0)
    t.fd(70)
    t.end_fill()


    my_goto(-103.42,15.09)
    t.fd(90)
    t.seth(70)
    t.fillcolor('#ffd200')#铃铛颜色
    t.begin_fill()
    t.circle(-20)
    t.end_fill()
    t.goto(-13.42,15.09)
    t.seth(250)
    t.circle(20,110)
    t.seth(90)
    t.fd(15)
    t.dot(10)
    my_goto(0,-150)
    t.seth(0)
    my_goto(-20,195)
    t.fillcolor('#000000')#眼睛颜色
    t.begin_fill()
    t.circle(13)
    t.end_fill()
    t.pensize(6)
    my_goto(20,205)
    t.seth(75)
    t.circle(-10,150)
    t.pensize(3)
    my_goto(-17,200)
    t.seth(0)
    t.fillcolor('#ffffff')#眼睛里的光颜色
    t.begin_fill()
    t.circle(5)
    t.end_fill()
    my_goto(0,0)
    t.mainloop()
threads = []
threads.append(threading.Thread(target=play_music))
threads.append(threading.Thread(target=painting))
if __name__ == '__main__':
    for t in threads:
        t.start()
'''
t.seth(240)
t.fd(40)
#t.goto(-170,-40)
#t.goto(100,100)
t.fillcolor('#ffffff')#左手
t.begin_fill()
t.circle(30,250)
t.end_fill()
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242

5小时后更新
折腾了几个小时完全修复了空白,完美播放音乐,可以打包成有依赖的exe文件,这是送给家人的生日礼物,太累了在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/983462
推荐阅读
相关标签
  

闽ICP备14008679号