Search code examples
pythonpygamepygtk

Pygame and PyGTK side by side


I'm working on a python project where I have a pygame window, but I'd also like to have a PyGTK window next to it at the same time with information about objects inside the pygame window. However, when I start the PyGTK window the pygame window freezes until the PyGTK one is closed, even if I do all the PyGTK stuff in a thread.

enter image description here Important pieces of code from my project:

import thread
import pygtk
import gtk

class SelectList:
    def __init__(self, parent):
        self.parent = parent
        self.initWindow()
        self.main()

    def main(self):
        gtk.main()

    def initWindow(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.window.connect("destroy", self.destroy)

        self.window.set_title("Selection info")
        self.window.set_position(gtk.WIN_POS_CENTER)

        # junk that I didn't bother including in this example
        # ...

        self.window.show_all()

    #This is only connected to the main window
    #When this is called it iterates through every toplevel window and closes it
    def destroy(self, widget, data=None):
        for i in gtk.window_list_toplevels():
            i.destroy()
        gtk.main_quit()


def openList(instance):
    SelectList(instance)

class Maker:
    def __init__(self):
        # more stuff that I won't include
        pass

    def logic(self, newkeys):
        if K_l in newkeys:
            thread.start_new_thread(openList, (self, ))

    def main(self):
        while True:
            # pygame stuff, including sending all new key presses to logic method
            pass

Solution

  • I looked over the stack overflow question OnGle mentioned in his answer, and saw two solutions that I feel like I'd be able to implement.


    Solution 1

    https://stackoverflow.com/a/199288/4468084 I could keep the PyGTK portion of the program in a separate process altogether, and just send data between the two currently running programs. However, this seems a bit overboard for what I need.

    Solution 2

    I could use either PGU or OcempGUI, which are both Pygame libraries meant to simplify low-level graphic programming stuff. Using one of these, I could forget about using PyGTK entirely and just use the premade objects from them.


    Summary

    While solution 1 keeps my original PyGTK implementation in mind, it also might be overcomplicated for such a simple program that I'm making. On top of that, using solution 1 would also mean I'd have to continue having two windows open at once when my program is ran, making it seem cluttered and bulky.

    Solution 2, on the other hand, cleanly integrates the object list into my Pygame project, while also lessening the amount of code I have to make. I also don't have to worry about different operating systems handling data transfers differently as would probably be a problem with solution 1. In the end, this seems like the better decision my situation.