温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Python中使用tkinter和exe打包

发布时间:2021-05-21 18:04:47 来源:亿速云 阅读:1024 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关怎么在Python中使用tkinter和exe打包,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

  界面如下:

class Application(tk.Frame):
  def __init__(self, master=None):
    tk.Frame.__init__(self, master)
    self.pack()
    self.createWidgets()
  def __del__(self):
    print('del tianyanche')
    root.destroy
  def createWidgets(self):
    self.labelDest = tk.Label(self)
    self.labelDest["text"] = '来源路径:'
    self.labelDest.grid(row=0, column=0)
    self.contentDest = tk.StringVar()
    self.contentDest.set('./work/all.txt')
    self.entryDest = tk.Entry(self)
    self.entryDest["textvariable"] = self.contentDest
    self.entryDest.grid(row=0, column=1)
    self.labelSuc = tk.Label(self)
    self.labelSuc["text"] = '成功路径:'
    self.labelSuc.grid(row=1, column=0)
    self.contentSuc = tk.StringVar()
    self.contentSuc.set('./work/suc.txt')     #设置界面的默认值
    self.entrySuc = tk.Entry(self)
    self.entrySuc["textvariable"] = self.contentSuc
    self.entrySuc.grid(row=1, column=1)
    self.labelFail = tk.Label(self)
    self.labelFail["text"] = '失败路径:'      #设置界面的默认值
    self.labelFail.grid(row=2, column=0)
    self.contentFail = tk.StringVar()
    self.contentFail.set('./work/fail.txt')    #设置界面的默认值
    self.entryFail = tk.Entry(self)
    self.entryFail["textvariable"] = self.contentFail
    self.entryFail.grid(row=2, column=1)
    self.buttonStart = tk.Button(self, text = '开始')
    self.buttonStart['command'] = self.start
    self.buttonStart['fg'] = 'green'
    self.buttonStart.grid(row=3, column=0)
    self.quit = tk.Button(self, text="停止", fg="red",
               command=self.quit)
    self.quit.grid(row=3, column=1)
    self.text = ScrolledText(self)
    self.text.grid(row=4, columnspan=2)
  def start(self):
    self.running = True
    self.text.insert('end', '来源:' + self.contentDest.get() + "\r\n")
    self.text.insert('end', '成功:' + self.contentSuc.get() + "\r\n")
    self.text.insert('end', '失败:' + self.contentFail.get() + "\r\n")
    self.tianyancha = TianYanCha.TianYanCha(self.contentSuc.get(), self.contentFail.get())
    self.tianyancha.setOutput(self.text)
    self.td = threading.Thread(target=self.startThread)
    self.td.setDaemon(True)
    self.td.start()

  def quit(self):
    self.running = False
    del self.tianyancha
    print('quit')

  def startThread(self):
    self.text.delete(0.0, 'end')
    file = open(self.contentDest.get())
    for line in file.readlines():
      if self.running == True:
        self.tianyancha.getCompanyByName(line.strip('\n'))
      else:
        print('停止')
        break;

以上就是界面部分,效果如下图所示:

怎么在Python中使用tkinter和exe打包

##exe打包
  Python的常用exe打包有:PyInstaller、py2exe等。我直接选择了PyInstaller。

PyInstaller安装:pip install PyInstaller。目前的最新版本3.2。
PyInstaller打包命令:pyinstaller -F -i snail.ico Application.py。

命令释义:

| 参数|含义|
| :-------------: |:-------------|
|-F|指定打包后只生成一个exe格式的文件|
|-D|–onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)|
|-c|–console, –nowindowed 使用控制台,无界面(默认)|
|-w|–windowed, –noconsole 使用窗口,无控制台|
|-p|添加搜索路径,让其找到对应的库|
|-i|改变生成程序的icon图标|

python有哪些常用库

python常用的库:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

以上就是怎么在Python中使用tkinter和exe打包,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI