鱼C论坛

 找回密码
 立即注册
查看: 966|回复: 3

[已解决]我的Python程序在用户的电脑上就不能运行

[复制链接]
发表于 2022-2-11 08:54:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
就是这段代码
  1. import os
  2. import sys
  3. import time
  4. import traceback
  5. from tkinter import *
  6. from tkinter.font import Font
  7. from tkinter.ttk import *
  8. from tkinter.messagebox import *


  9. try: # 定位WeChat Files文件夹
  10.     users = os.path.expandvars('$HOMEPATH')
  11.     f = open(r'C:' + users + '\\AppData\\Roaming\\Tencent\\WeChat\\All Users\\config\\3ebffe94.ini')
  12.     f1 = f.read()
  13.     if f1 == 'MyDocument:':
  14.         location = 'C:' + users + '\Documents\WeChat Files'
  15.     else:
  16.         location = f1 + "\WeChat Files"
  17.     list0 = os.listdir(location)
  18.     list0.remove('All Users')
  19.     list0.remove('Applet')
  20.     title = "(状态正常)"
  21.     errorcode = 0
  22. except Exception as reason:
  23.     errorcode = 1
  24.     title = "(错误)"
  25.     erroreason = '错误原因是:' + str(traceback.format_exc())

  26. class Application_ui(Frame): # 生成GUI界面
  27.     def __init__(self, master=None):
  28.         Frame.__init__(self, master)
  29.         self.master.title('Windows微信清理工具' + title + '   yunlongzhuhuo[52pojie.cn]')
  30.         sw = master.winfo_screenwidth() #得到屏幕宽度
  31.         sh = master.winfo_screenheight() #得到屏幕高度
  32.         ww = 621
  33.         wh = 308
  34.         x = (sw-ww) / 2
  35.         y = (sh-wh) / 2
  36.         self.master.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
  37.         self.createWidgets()

  38.     def createWidgets(self):
  39.         try:
  40.             self.top = self.winfo_toplevel()

  41.             self.style = Style()

  42.             self.Command1Var = StringVar(value='清理')
  43.             self.style.configure('TCommand1.TButton', font=('微软雅黑',10))
  44.             self.Command1 = Button(self.top, text='清理', textvariable=self.Command1Var, command=self.cleaner, style='TCommand1.TButton')
  45.             self.Command1.setText = lambda x: self.Command1Var.set(x)
  46.             self.Command1.text = lambda : self.Command1Var.get()
  47.             self.Command1.place(relx=0.386, rely=0.701, relwidth=0.195, relheight=0.159)

  48.             self.Combo2List = ('聊天记录','图片和视频','接收到的文件','30天前的图片和视频','30天前接收到的文件','全部数据')
  49.             self.Combo2Var = StringVar(value='')
  50.             self.Combo2 = Combobox(self.top, text='Add items in designer or code!', textvariable=self.Combo2Var, values=self.Combo2List, font=('微软雅黑',9))
  51.             self.Combo2.setText = lambda x: self.Combo2Var.set(x)
  52.             global choice
  53.             choice = lambda : self.Combo2Var.get()
  54.             self.Combo2.place(relx=0.412, rely=0.416, relwidth=0.53)

  55.             global list0
  56.             self.Combo1List = tuple(list0)
  57.             self.Combo1Var = StringVar(value='')
  58.             self.Combo1 = Combobox(self.top, text='Add items in designer or code!', textvariable=self.Combo1Var, values=self.Combo1List, font=('微软雅黑',9))
  59.             self.Combo1.setText = lambda x: self.Combo1Var.set(x)
  60.             global wxid
  61.             wxid = lambda : self.Combo1Var.get()
  62.             self.Combo1.place(relx=0.412, rely=0.13, relwidth=0.53)

  63.             self.Label2Var = StringVar(value='请选择你要清理的内容:')
  64.             self.style.configure('TLabel2.TLabel', anchor='center', font=('微软雅黑',10))
  65.             self.Label2 = Label(self.top, text='请选择你要清理的内容:', textvariable=self.Label2Var, style='TLabel2.TLabel')
  66.             self.Label2.setText = lambda x: self.Label2Var.set(x)
  67.             self.Label2.text = lambda : self.Label2Var.get()
  68.             self.Label2.place(relx=0.063, rely=0.416, relwidth=0.248, relheight=0.065)

  69.             self.Label1Var = StringVar(value='请选择你要清理的微信号:')
  70.             self.style.configure('TLabel1.TLabel', anchor='center', font=('微软雅黑',10))
  71.             self.Label1 = Label(self.top, text='请选择你要清理的微信号:', textvariable=self.Label1Var, style='TLabel1.TLabel')
  72.             self.Label1.setText = lambda x: self.Label1Var.set(x)
  73.             self.Label1.text = lambda : self.Label1Var.get()
  74.             self.Label1.place(relx=0.052, rely=0.13, relwidth=0.271, relheight=0.065)

  75.             global errorcode
  76.             errorcode = 0
  77.         except Exception as reason:
  78.             errorcode = 1
  79.             erroreason = '错误原因是: \n' + str(traceback.format_exc())

  80.         if errorcode == 0:
  81.             showinfo(title = 'Windows微信清理工具   yunlongzhuhuo[52pojie.cn]',message = '欢迎使用此工具!\n WeChat Files文件夹定位成功: %s \n 工具可以正常使用!' % location)
  82.         if errorcode == 1:
  83.             showerror(title = 'Windows微信清理工具   yunlongzhuhuo[52pojie.cn]',message = '欢迎使用此工具!\n 很抱歉,WeChat Files文件夹定位失败! \n 工具不能使用!\n %s \n 请反馈给开发者!' % erroreason)

  84.     def cleaner(self):

  85.         def delete(files): # 清空文件夹函数
  86.             ask = askokcancel(title = 'Windows微信清理工具',message = '确认清理?')
  87.             if ask == True:
  88.                 os.system('del /f /s /q "%s\\*.*"' % files)
  89.                 showinfo(title = 'Windows微信清理工具',message = '清理成功!')

  90.         def del_time_item(locations): # 删除文件函数
  91.             ask = askokcancel(title = 'Windows微信清理工具',message = '确认清理?')
  92.             if ask == True:
  93.                 os.system('del /f /s /q "%s"' % locations)
  94.                 showinfo(title = 'Windows微信清理工具',message = '清理成功!')


  95.         def timecleaner(fileslocation): # 遍历查找30天前的文件并删除
  96.             try:
  97.                 nowtime = time.time()
  98.                 os.chdir(fileslocation)
  99.                 for root, dirs, files in os.walk(fileslocation):
  100.                     for file in files:
  101.                         fileslist = []
  102.                         fileslist.append(os.path.join(root,file))
  103.                         for fileslist1 in fileslist:
  104.                             filestime = os.path.getmtime(fileslist1)
  105.                             timelist = []
  106.                             time_until_now = nowtime - filestime
  107.                             timelist.append(time_until_now)
  108.                             dict0 = dict(zip(fileslist,timelist))
  109.                             if time_until_now < 2592000:
  110.                                 timelist.remove(time_until_now)
  111.                             for delitem in timelist:   
  112.                                 delfile = list(dict0.keys())[list(dict0.values()).index(delitem)]
  113.                                 del_time_item(delfile)
  114.             except Exception as reason:
  115.                 showerror(title = 'Windows微信清理工具',message = '出错啦~~ \n 错误原因是:\n %s \n 请反馈给开发者!' % traceback.format_exc())
  116.                

  117.         def getwxid(): # 获得微信号
  118.             return wxid()

  119.         def getchoice(): # 获得路径
  120.             return choice()

  121.         if getchoice() == '聊天记录':           # 具体删除功能
  122.             dialog = location + "\" + getwxid() + '\Msg'
  123.             delete(dialog)
  124.         elif getchoice() == '图片和视频':
  125.             pictures = location + "\" + getwxid() + '\FileStorage\Image'
  126.             delete(pictures)
  127.             videos = location + "\" + getwxid() + '\FileStorage\Video'
  128.             delete(videos)
  129.         elif getchoice() == '接收到的文件':
  130.             documents = location + "\" + getwxid() + '\FileStorage\File'
  131.             delete(documents)
  132.         elif getchoice() == '全部数据':
  133.             delall = location + "\" + getwxid()
  134.             delete(delall)
  135.         elif getchoice() == '30天前的图片和视频':
  136.             pictures = location + "\" + getwxid() + '\FileStorage\Image'
  137.             videos = location + "\" + getwxid() + '\FileStorage\Video'
  138.             timecleaner(pictures)
  139.             timecleaner(videos)
  140.         elif getchoice() == '30天前接收到的文件':
  141.             documents = location + "\" + getwxid() + '\FileStorage\File'
  142.             timecleaner(documents)

  143. class Application(Application_ui):
  144.     def __init__(self, master=None):
  145.         Application_ui.__init__(self, master)

  146. if __name__ == "__main__":
  147.     top = Tk()
  148.     Application(top).mainloop()
复制代码


报错信息:

                               
登录/注册后可看大图


为什么?为什么?为什么在我自己的电脑上就没问题?
最佳答案
2022-2-11 17:55:55
本帖最后由 isdkz 于 2022-2-11 17:57 编辑

因为你的11行的那个try里面的代码出错了,导致list0未定义,

而有可能会出错的地方就是13行和19行,因为目录或文件不存在而导致出错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-11 17:55:55 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2022-2-11 17:57 编辑

因为你的11行的那个try里面的代码出错了,导致list0未定义,

而有可能会出错的地方就是13行和19行,因为目录或文件不存在而导致出错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-11 18:13:24 From FishC Mobile | 显示全部楼层
isdkz 发表于 2022-2-11 17:55
因为你的11行的那个try里面的代码出错了,导致list0未定义,

而有可能会出错的地方就是13行和19行,因为 ...

但是如果文件或目录不存在的话,应该报FileNotFoundError,不应该报NameError啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-11 18:50:03 | 显示全部楼层
本帖最后由 isdkz 于 2022-2-11 18:54 编辑
云龙烛火 发表于 2022-2-11 18:13
但是如果文件或目录不存在的话,应该报FileNotFoundError,不应该报NameError啊


在try里面出错都被捕获了,当然不会报FileNotFoundError了,

但是会导致try语句块里面的变量没有成功定义,

后面要用到的时候因为变量没有定义,就报NameError了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 10:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表