8

I am using pygame to write a program and I need some GUI configuration text field and button for control. I've already made the button using pygame, but I just can write a text field out of pygame. Maybe I need to use tkinter together with pygame.

I think if there is no way to made to pygame part and tkinter part together in 1 window, then I could put them into 2 separate windows.

I hope the tkinter part can update the global variable in my pygame part, would there if any problem? I might create a child process of tkinter from the pygame part so that the tkinter part can probably "see" the global variable in pygame part and modify them.

Can I do this? Are there any pitfalls?

lamwaiman1988
  • 3,729
  • 15
  • 55
  • 87

3 Answers3

9

Both Tkinter and Pygame have their own event loops, so doing what you want is far from simple. The problem is that Pygame wants to control both the screen and the events the user feeds into the computer. This doesn't work well with GUI libraries, which also want to be "in control".

I would suggest sticking with Pygame, it has some nice GUI toolkits that will help you create buttons and other controls. Go over this page - it should clear things out. You may also find this discussion useful.

Apart from the practical aspects, a GUI created with Pygame is also IMHO more suitable for a game than something done with Tkinter, since games usually have original, thematical user interfaces and not the bland "text box + button" windows we're used to in other applications.

Take a look at some of the sample games on the Pygame wiki, many have GUIs and you can borrow ideas and code from them.

Community
  • 1
  • 1
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 1
    Pygame does not have an event loop - it expects you to write your own event loop - so It would be ok to create a Tkinter app from inside a pygame app, except if one is usign fullscreen for the pygame. But you are right int hat the user shoudl use some toolkit for pygame. – jsbueno Nov 28 '11 at 12:19
  • @jsbueno: once you call into Tkinter, its event loop takes control. So how do you run it from a running pygame loop? – Eli Bendersky Nov 28 '11 at 15:35
  • the pgu toolkit sucks. No tutorial and I am just trying by myself. Now I think it is working and suddenly the window just freeze without reason/exception. – lamwaiman1988 Nov 28 '11 at 16:14
3
    from tkinter import *
    import pygame
    import random
    import os
    global playing
    playing=False
    def playpause():
        global playing
        if playing==True:
            playing=False
        else:
            playing=True
    root = Tk()
    embed = Frame(root, width=640, height=480)
    embed.grid(row=0,column=2)
    playpausebutton=Button(root, command=playpause, text="Play/Pause")
    playpausebutton.grid(row=1,column=2)
    root.update()
    os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
    os.environ['SDL_VIDEODRIVER'] = 'windib'
    pygame.display.init()
    screen = pygame.display.set_mode((640,480))
    pygame.display.flip()
    while True:
        #your code here
        if playing: 
                screen.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
        pygame.display.flip()
        root.update()

This works just great, I have used this method successfully in multiple cases.

  • This worked for me. The only problem I ran into was, I had to comment out the line that sets the video driver to 'windib', or I get an error from pygame saying "No available video device". (I also get a complaint on exit but that's not really a problem.) – hosford42 May 01 '20 at 02:45
  • I submitted an edit to resolve the error and the warning on exit. I also added a call to root.update_idletasks() since this is apparently normally called by mainloop(). – hosford42 May 01 '20 at 03:19
1

I have also found pgu is awful. However, what you say about the tkinter event loop taking control is wrong. You just call root.update instead of mainloop, and this can go inside of a while loop to replace the mainloop. Answering your main question however, there is something you should be aware of. It seems that whenever I run the two programs alongside each other, this traceback occurs:

TclError: expected boolean value but got "-1"

Fatal Python error: (pygame parachute) Segmentation Fault

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

Because of this I would avoid the combination, although I suspect this particular issue may pertain to my use of threads.

trevorKirkby
  • 1,886
  • 20
  • 47