Cross-platform gui toolkit for deploying Python applications

后端 未结 5 846
情深已故
情深已故 2020-11-30 17:46

Building on: http://www.reddit.com/r/Python/comments/7v5ra/whats_your_favorite_gui_toolkit_and_why/

Merits:

1 - ease of design / integration - learning curve

5条回答
  •  -上瘾入骨i
    2020-11-30 18:50

    Jython.

    Jython is an implementation of the high-level, dynamic, object-oriented language Python written in 100% Pure Java, and seamlessly integrated with the Java platform. It thus allows you to run Python on any Java platform.

    You can use either Swing, Applet, or other GUI frameworks available to Java platform. See Java Tutorials for Graphical User Interfaces and 2D Graphics. There are plenty of books and documentation such as API reference.

    Here's a Hello world Swing application from An Introduction to Jython.

    from javax.swing import *
    
    frame = JFrame("Hello Jython")
    label = JLabel("Hello Jython!", JLabel.CENTER)
    frame.add(label)
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    frame.setSize(300, 300)
    frame.show()
    

    Here's a Jython applet by Todd Ditchendorf that demonstrates multi-threaded particle drawing (60 lines).

    from __future__ import nested_scopes
    import java.lang as lang
    import java.util as util
    import java.awt as awt
    import javax.swing as swing
    
    class Particle:
    
        def __init__(self,initX,initY):
        self.x = initX
        self.y = initY
        self.rng = util.Random()
    
        def move(self):
        self.x += self.rng.nextInt(10) - 5
        self.y += self.rng.nextInt(20) - 10
    
        def draw(self,g2):
        g2.drawRect(self.x,self.y,10,10)
    
    class ParticleCanvas(awt.Canvas):
    
        def __init__(self,newSize):
        awt.Canvas.__init__(self,size=(newSize,newSize))
    
        def paint(self,g2):
        for p in self.particles:
            p.draw(g2)
    
    class ParticleApplet(swing.JApplet):
    
        def init(self):
        self.canvas = ParticleCanvas(self.getWidth())
        self.contentPane.add(self.canvas)
    
        def start(self):
        n = 10
        particles = []
        for i in range(n):
            particles.append(Particle(150,150))
        self.canvas.particles = particles
    
        self.threads = []
        for i in range(n):
            self.threads.append(self.makeThread(particles[i]))
            self.threads[i].start()
    
        def makeThread(self,p):
    
        class MyRunnable(lang.Runnable):
            def run(this):
            try:
                while 1:
                p.move()
                self.canvas.repaint()
                lang.Thread.sleep(100)
            except lang.InterruptedException:
                return
    
        return lang.Thread(MyRunnable())
    

    If you are just interested in drawing lines and circles you can probably cut it down to half.

提交回复
热议问题