Skip to content

Instantly share code, notes, and snippets.

@secemp9
Last active August 22, 2022 17:00
Show Gist options
  • Save secemp9/e581411cddac7c402fef42e177edb9b2 to your computer and use it in GitHub Desktop.
Save secemp9/e581411cddac7c402fef42e177edb9b2 to your computer and use it in GitHub Desktop.
pygame + tkinter on separate threads...because why not

So this is just me experimenting...

Anyway, there a peculiar error without the if block at 69 LOC. If you keep everything but just remove the if condition + the time.sleep, then proceed to decrement/increment the variable "width" using the tkinter buttons "Up" and "Down" fast enough (I used two fingers because there no key repetitoon support on the tkinter function I made), you get this:

Process finished with exit code -1073740791 (0xC0000409)

After a while, I found that the likely cause is one of the variable/memory part of pygame/tkinter that try to access something that isn't likely there, or not "ready" yet? (eg: not initialized, etc). But on retrospect, I probably don't know the exact cause. Here an example I found on stackoverflow: https://stackoverflow.com/questions/50562192/process-finished-with-exit-code-1073740791-0xc0000409-pycharm-error

There also a visual glitch where the label/text get erased/become black, or just isn't there anymore. Related: https://stackoverflow.com/questions/48492273/preloading-windows-to-avoid-tkinter-visual-glitches-during-frame-load

import time
from tkinter import *
import pygame
import sys
import threading
pygame.init()
timer = pygame.time.Clock()
fps = 60
#pygame.key.set_repeat(1, 10)
width = 1200
width_wow = 250
change_happen = 0
def tk_repl():
global width, width_wow, change_happen
root = Tk()
root.title("Program")
root.geometry('350x250')
def resize(event):
global width_wow
if width_wow != root.winfo_width():
width_wow = root.winfo_width()
print(root.winfo_width())
label = Label(root, text="width")
label.grid(column=0, row=0, sticky='w', columnspan=width_wow)
label = Label(root, text=width)
label.grid(column=1, row=1, sticky='w', columnspan=width_wow)
def clicked_up():
global width, change_happen
width += 1
change_happen = 1
#time.sleep(0.1)
label.config(text=width)
def clicked_down():
global width, change_happen
width -= 1
change_happen = 1
#time.sleep(0.1)
label.config(text=width)
button1 = Button(root, text="Up", command=clicked_up, anchor="w")
button1.grid(column=2, row=2)
button2 = Button(root, text="Down", command=clicked_down, anchor="w")
button2.grid(column=3, row=2)
root.bind("<Configure>", resize)
root.mainloop()
height = 300
screen = pygame.display.set_mode([width, height])
second_surface = pygame.Surface([width, height])
second_surface.fill((0,0,255))
#second_surface_wow = second_surface.get_rect(topleft = [width, height])
pygame.display.set_caption('side scroller')
imgPos = pygame.Rect((0, 0), (0, 0))
def pixel(yep, second_surface, color="yellow"):
print(yep)
second_surface.fill(color, (yep, (10, 10)))
wow = threading.Thread(target=tk_repl)
wow.start()
run = True
while run:
if change_happen == 1:
timer.tick(fps)
screen = pygame.display.set_mode([width, height])
screen.blit(second_surface, imgPos)
pygame.display.flip()
change_happen = 0
else:
time.sleep(0.01)
pygame.quit()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment