Create a directly-executable cross-platform GUI app using Python

前端 未结 12 2175
予麋鹿
予麋鹿 2020-11-22 10:52

Python works on multiple platforms and can be used for desktop and web applications, thus I conclude that there is some way to compile it into an executable for Mac, Windows

相关标签:
12条回答
  • 2020-11-22 11:20

    I'm not sure that this is the best way to do it, but when I'm deploying Ruby GUI apps (not Python, but has the same "problem" as far as .exe's are concerned) on Windows, I just write a short launcher in C# that calls on my main script. It compiles to an executable, and I then have an application executable.

    0 讨论(0)
  • 2020-11-22 11:20

    PySimpleGUI wraps tkinter and works on Python 3 and 2.7. It also runs on Qt, WxPython and in a web browser, using the same source code for all platforms.

    You can make custom GUIs that utilize all of the same widgets that you find in tkinter (sliders, checkboxes, radio buttons, ...). The code tends to be very compact and readable.

    #!/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()
    

    As explained in the PySimpleGUI Documentation, to build the .EXE file you run:

    pyinstaller -wF MyGUIProgram.py

    0 讨论(0)
  • 2020-11-22 11:22

    You can use appJar for basic GUI development.

    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()
    

    See documentation at appJar site.

    Installation is made with pip install appjar from command line.

    0 讨论(0)
  • 2020-11-22 11:26
    # 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

    1) Create a setup.py

    with this code:

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

    save it in a folder

    2) 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)

    3) Run cmd from that folder (on the folder, right click + shift and choose start cmd here)
    4) write in cmd:>python setup.py py2exe
    5) in the dist folder there are all the files you need
    6) 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

    **

    0 讨论(0)
  • 2020-11-22 11:26

    You don't need to compile python for Mac/Windows/Linux. It is an interpreted language, so you simply need to have the Python interpreter installed on the system of your choice (it is available for all three platforms).

    As for a GUI library that works cross platform, Python's Tk/Tcl widget library works very well, and I believe is sufficiently cross platform.

    Tkinter is the python interface to Tk/Tcl

    From the python project webpage:

    Tkinter is not the only GuiProgramming toolkit for Python. It is however the most commonly used one, and almost the only one that is portable between Unix, Mac and Windows

    0 讨论(0)
  • 2020-11-22 11:30

    There's also PyGTK, which is basically a Python wrapper for the Gnome Toolkit. I've found it easier to wrap my mind around than Tkinter, coming from pretty much no knowledge of GUI programming previously. It works pretty well and has some good tutorials. Unfortunately there isn't an installer for Python 2.6 for Windows yet, and may not be for a while.

    0 讨论(0)
提交回复
热议问题