相关文章推荐
侠义非凡的剪刀  ·  SQL ...·  1 月前    · 
开心的苦咖啡  ·  ORA-01830: ...·  1 年前    · 

使用Python创建一个可直接执行的跨平台GUI应用程序

300 人关注

Python可以在多个平台上工作,可以用于桌面和网络应用,因此我断定有一些方法可以将其编译成Mac、Windows和Linux的可执行文件。

问题是我不知道从哪里开始,也不知道如何用它写一个GUI,谁能给我一些启示,给我指出正确的方向?

python
user-interface
deployment
tkinter
release-management
Teifion
Teifion
发布于 2008-08-06
13 个回答

首先,你需要一些带有Python绑定的GUI库,然后(如果你愿意)需要一些将你的Python脚本转换为独立可执行文件的程序。

带有Python绑定的跨平台GUI库(Windows、Linux、Mac)。

当然,有很多,但我在野外看到的最受欢迎的是。

  • Tkinter - based on Tk GUI toolkit (de-facto standard GUI library for python, free for commercial projects)
  • WxPython - based on WxWidgets (popular, free for commercial projects)
  • Qt using the PyQt bindings or Qt for Python . The former is not free for commercial projects. The latter is less mature, but can be used for free.
  • 完整名单见 http://wiki.python.org/moin/GuiProgramming

    单一可执行文件(所有平台)

  • PyInstaller - the most active(Could also be used with PyQt )
  • fbs - if you chose Qt above
  • 单一可执行文件(Windows)。

  • py2exe - used to be the most popular
  • 单一可执行文件(Linux)

  • Freeze - works the same way like py2exe but targets Linux platform
  • 单一可执行文件(Mac)。

  • py2app - again, works like py2exe but targets Mac OS
  • 谈谈windows下的pyinstaller,而不是py2exe。
    CTT
    值得注意的是,从4.5开始,QT将采用LGPL。
    ... which is why you should now very much consider using PySide which is LGPL。它也比PyQt4的Python 2 API更加Pythonic。
    fbmd
    您还可以添加 cx_Freeze ,它是跨平台的,得到了维护,并支持Python 3.x。
    Why is there no mention of Kivy 这里?对于这个问题来说,它不是一个有效的选项吗?
    Jamie
    Jamie
    发布于 2021-04-10
    0 人赞同

    另一个系统(在接受的答案中还没有提到)是PyInstaller,当py2exe不能工作时,它为我的一个PyQt项目工作。我发现它更容易使用。

    http://www.pyinstaller.org/

    Pyinstaller是基于Gordon McMillan的Python Installer。它已不再可用。

    Dave
    在这个答案写了三年多之后,pyinstaller仍然不支持Python 3,也没有被列入未来版本的路线图中。 我的意思是,对 AIX 和 Solaris 的支持真的比 Python 3 更加有用吗?
    Katu
    它现在支持Python 2.7和Python 3.3、3.4和3.5。
    现在也是3.6。
    Michael Twomey
    Michael Twomey
    发布于 2021-04-10
    0 人赞同

    py2exe的一个替代工具是 bbfreeze 它为windows和linux生成可执行文件。它比py2exe要新,对鸡蛋的处理相当好。我发现它在没有配置的情况下,在各种应用中神奇地工作得更好。

    Tofystedeth
    Tofystedeth
    发布于 2021-04-10
    0 人赞同

    还有 PyGTK 它基本上是Gnome Toolkit的一个Python封装器。 我发现它比Tkinter更容易上手,因为我以前几乎没有GUI编程的知识。 它运行得很好,而且有一些很好的教程。 不幸的是,现在还没有Windows版Python 2.6的安装程序,而且可能在一段时间内也不会有。

    Matthew Schinckel
    Matthew Schinckel
    发布于 2021-04-10
    0 人赞同

    由于现在几乎所有的非Windows操作系统都默认安装了python,你真正需要确保的是你使用的所有非标准库都已安装。

    说到这里,我们有可能建立包括Python解释器和任何你使用的库的可执行文件。 然而,这可能会产生一个大的可执行文件。

    MacOS X甚至包括在Xcode IDE中支持创建完全独立的GUI应用程序。 这些可以由任何运行OS X的用户运行。

    fulmicoton
    fulmicoton
    发布于 2021-04-10
    0 人赞同

    For the GUI itself:

    PyQT is pretty much the reference.

    另一种开发快速用户界面的方法是编写一个网络应用。 让它在本地运行并在浏览器中显示该应用。

    另外,如果你选择lubos hasko建议的Tkinter方案的话 你可能想试试 portablepy 来让你的应用程序在 Windows 环境下运行 而不使用Python。

    Brian Warshaw
    Brian Warshaw
    发布于 2021-04-10
    0 人赞同

    我不确定这是否是最好的方法,但当我在Windows上部署Ruby GUI应用程序(不是Python,但就.exe而言有同样的 "问题")时,我只是用C#写一个简短的启动器,调用我的主脚本。它编译成一个可执行文件,然后我就有了一个可执行的应用程序。

    @Brian 你有没有看到 _why's technique 为自执行的Shoes应用程序?它可能可以用非Shoes程序来完成。
    对于这些应用程序,我的用户没有一个是匿名的。我知道他们是否有运行时,如果他们没有,我可以为他们安装它。这个话题的主要关注点是如何启动这些应用程序,而不必打开命令提示符,输入运行时的可执行文件和程序名称。
    Mike from PSG
    Mike from PSG
    发布于 2021-04-10
    0 人赞同

    PySimpleGUI包装了tkinter,可以在Python 3和2.7上运行。它也可以在Qt、WxPython和网络浏览器上运行,所有平台都使用相同的源代码。

    你可以利用所有你在tkinter中发现的同样的部件(滑块、复选框、单选按钮...)来制作自定义图形用户界面。 代码往往是非常紧凑和可读的。

    #!/usr/bin/env python
    import sys
    if sys.version_info[0] >= 3:
        import PySimpleGUI as sg
    else:
        import PySimpleGUI27 as sg
    layout = [[ sg.Text('My Window') ],
              [ sg.Button('OK')]]
    window = sg.Window('My window').Layout(layout)
    button, value = window.Read()
    

    看到没有人提到Kivy,我感到很惊讶!!。

    我曾经用Tkinter做过一个项目,虽然他们确实鼓吹Tkinter有了很大的改进,但还是给我一种windows98的感觉,所以我改用了Kivy.

    我一直在关注一个辅导系列 if it helps...

    为了让大家了解kivy的样子,请看这个(我正在做的项目)。

    而我已经为它工作了不到一个星期了!你问我对Kivy的好处是什么? 你问Kivy的好处是什么?请看this

    我选择这个的原因是,它的外观和它也可以在移动中使用。

    PythonProgrammi
    PythonProgrammi
    发布于 2021-04-10
    0 人赞同
    # I'd use tkinter for python 3
    import tkinter
    tk = tkinter.Tk()
    tk.geometry("400x300+500+300")
    l = Label(tk,text="")
    l.pack()
    e = Entry(tk)
    e.pack()
    def click():
        e['text'] = 'You clicked the button'
    b = Button(tk,text="Click me",command=click)
    b.pack()
    tk.mainloop()
    # After this I would you py2exe
    # search for the use of this module on stakoverflow
    # otherwise I could edit this to let you know how to do it
    

    py2exe

    Then you should use py2exe, for example, to bring in one folder all the files needed to run the app, even if the user has not python on his pc (I am talking of windows... for the apple os there is no need of an executable file, I think, as it come with python in it without any need of installing it.

    Create this file

  • Create a setup.py
  • with this code:

    from distutils.core import setup
    import py2exe
    setup(console=['l4h.py'])
    

    save it in a folder

  • Put your program in the same folder of setup.py put in this folder the program you want to make it distribuitable: es: l4h.py
  • ps: change the name of the file (from l4h to anything you want, that is an example)

  • Run cmd from that folder (on the folder, right click + shift and choose start cmd here)
  • write in cmd:>python setup.py py2exe
  • in the dist folder there are all the files you need
  • you can zip it and distribute it
  • Pyinstaller

    Install it from cmd

    pip install pyinstaller

    Run it from the cmd from the folder where the file is

    pyinstaller file.py

    Update

    阅读这篇帖子,用pyinstaller在windows上制作一个exe,并在其中包含一个文件和图像。https://pythonprogramming.altervista.org/auto-py-to-exe-only-one-file-with-images-for-our-python-apps/

    Justin Standard
    Justin Standard
    发布于 2021-04-10
    0 人赞同

    You don't need to compile python用于Mac/Windows/Linux。 它是一种解释语言,所以你只需要在你选择的系统上安装Python解释器(它对这三种平台都可用)。

    至于一个可以跨平台工作的GUI库,Python的 Tk/Tcl 小部件库工作得非常好,我相信它有足够的跨平台性。

    Tkinter 是Tk/Tcl的python接口。

    来自python项目的网页。

    Tkinter并不是唯一的GuiProgramming 的工具箱。 但它是 最常用的一个,而且几乎是 是唯一可以在 Unix、Mac和Windows之间移植。

    那么,你如何制作一个安装包来部署在那些只想进行1步安装的人的系统上(例如,在Windows中双击一个安装包)?
    对于这一点,我知道有几种方法:你可能会在批处理脚本中编写一个安装程序,或者为目标系统编译,将所需的文件放在某个地方,并在这个过程中安装所需的库和运行时。
    将 Python 作为你自己安装的一部分来安装会有点麻烦,因为你必须处理已经安装了错误版本的 Python 的可能性。你必须并排安装,这样你的应用程序可以访问新安装的 Python 版本,但任何现有的应用程序都继续使用旧版本的 Python。(也就是说,不要搞乱像 .py 文件关联或 PATH 这样的东西)
    更有可能的是,你的安装程序会检查是否存在正确的Python版本,如果它找到了,就使用它,否则就把Python作为安装过程的一部分来安装。
    是的--但是第二个选项 "以其他方式安装python作为安装过程的一部分",正是我所说的。由于我给出的原因,这将是很麻烦的。
    shahar_m
    shahar_m
    发布于 2021-04-10
    0 人赞同

    你可以使用 appJar 进行基本的GUI开发。

    from appJar import gui
    num=1
    def myfcn(btnName):   
        global num
        num +=1
        win.setLabel("mylabel", num)
    win = gui('Test')
    win.addButtons(["Set"],  [myfcn])
    win.addLabel("mylabel", "Press the Button")
    win.go()