May-01-2019, 09:33 AM
Hi everyone,
Can anyone help me so the code works with pygame and Tkinter (seemingly) simultaneously? I'm also interested in formatting the text and using the appropriate methods, anything to assist would be greatly appreciated .
I'm working on introducing Tkinter into my pygame project to make it easier to test and debug. I normally send the information to the Python shell window but thought it would be much cooler to have the information neatly displayed in a seperate window, ideally having the pygame window doing its thing, while keeping an eye on (some of) the variables in the Tkinter window.
I've done the pygame stuff already , and have got as far as launching the Tkinterwindow (incorrectly I imagine).
Can anyone help me so the code works with pygame and Tkinter (seemingly) simultaneously? I'm also interested in formatting the text and using the appropriate methods, anything to assist would be greatly appreciated .
I'm working on introducing Tkinter into my pygame project to make it easier to test and debug. I normally send the information to the Python shell window but thought it would be much cooler to have the information neatly displayed in a seperate window, ideally having the pygame window doing its thing, while keeping an eye on (some of) the variables in the Tkinter window.
I've done the pygame stuff already , and have got as far as launching the Tkinterwindow (incorrectly I imagine).
import tkinter import pygame pygame.init() pwbr_power = 0 pwbr_duration = 0 pwbr_scrn_refresh = 0 pwbr_elps_time = 0 pwbr_ttl_sec = 0 def fn_pbtkwin_update(): """ Updates the status information in the tkinter window. """ global pwbr_power, pwbr_duration, pwbr_scrn_refresh, pwbr_elps_time global pwbr_ttl_sec pwbr_power += 1 pwbr_duration += 0.15 pwbr_elps_time += pwbr_duration pwbr_scrn_refresh = 0.333 pwbr_ttl_sec += pwbr_elps_time if pwbr_elps_time > pwbr_scrn_refresh: pwbr_elps_time = 0 pwbr_duration = 0 caption_pwr = "Power: " + str(pwbr_power) caption_dur = "Duration: " + str(pwbr_duration) caption_rfrsh = "Screen Refresh: " + str(pwbr_scrn_refresh) caption_etime = "Elapsed time: " + str(pwbr_elps_time) caption_ttl = "Total sec's: " + str(pwbr_ttl_sec) tkinter.Label(tkwin, text=caption_pwr, fg="black").grid(row=0, column=0) tkinter.Label(tkwin, text=caption_dur, fg="black").grid(row=1, column=0) tkinter.Label(tkwin, text=pwbr_scrn_refresh, fg="black").grid(row=2, column=0) tkinter.Label(tkwin, text=caption_etime, fg="black").grid(row=3, column=0) tkinter.Label(tkwin, text=caption_ttl, fg="black").grid(row=4, column=0) tkwin.after(1000, fn_pbtkwin_update) # fn_pbtkwin_update() ---------------------------------------------------------- tkwin = tkinter.Tk() tkwin.title("Power Bar") tkinter.Label(tkwin, text = "Power: 0", fg = "black").grid(row=0, column=0) tkinter.Label(tkwin, text = "Duration: 0", fg = "black").grid(row=1, column=0) tkinter.Label(tkwin, text = "Screen Refresh: 0", fg = "black").grid(row=2, column=0) tkinter.Label(tkwin, text = "Elapsed time: 0", fg = "black").grid(row=3, column=0) tkinter.Label(tkwin, text = "Total sec's: 0", fg = "black").grid(row=4, column=0) tkinter.Label(tkwin, text = "Fully charged: False", fg = "black").grid(row=0, column=3) tkinter.Label(tkwin, text = "Reactor overloading: False", fg = "black").grid(row=1, column=3) tkinter.Label(tkwin, text = "Power bar status: Normal", fg = "black").grid(row=2, column=3) tkinter.Label(tkwin, text = "Press [Spacebar] to simulate firing phaser cannons.", fg = "black").grid(row=6, column=1) tkinter.Label(tkwin, text = "Press [r] to initiate a reactor overload.", fg = "black").grid(row=7, column=1) tkinter.Label(tkwin, text = "Press [Esc] to quit.", fg = "black").grid(row=7, column=1) tkwin.after(1000, fn_pbtkwin_update) pygame.time.wait(1000) pygame.display.set_mode((640, 480)) quit_loop = False while quit_loop is not True: for event in pygame.event.get(): if event.type == pygame.QUIT: # If user clicked close quit_loop = True # Flag that we are done so we exit this loop elif event.type == pygame.MOUSEBUTTONUP: pass elif event.type == pygame.constants.KEYDOWN: if event.key == pygame.constants.K_ESCAPE: quit_loop = True break elif event.key == pygame.constants.K_LEFT: kbd["left"] = True elif event.key == pygame.constants.K_RIGHT: kbd["right"] = True elif event.key == pygame.constants.K_UP: kbd["up"] = True elif event.key == pygame.constants.K_DOWN: kbd["down"] = True elif event.key == pygame.constants.K_SPACE: # Fire. kbd["fire"] = True elif event.key == pygame.constants.K_LCTRL: # Thrust. kbd["thrust"] = True elif event.key == pygame.constants.K_r: kbd["overload"] = True elif event.type == pygame.constants.KEYUP: if event.key == pygame.constants.K_LEFT: kbd["left"] = False elif event.key == pygame.constants.K_RIGHT: kbd["right"] = False elif event.key == pygame.constants.K_UP: kbd["up"] = False elif event.key == pygame.constants.K_DOWN: kbd["down"] = False elif event.key == pygame.constants.K_SPACE: # Fire. kbd["fire"] = False elif event.key == pygame.constants.K_LCTRL: # Thrust. kbd["thrust"] = False elif event.key == pygame.constants.K_r: kbd["overload"] = False # end if # end if # end for loop # { Paint some graphics on the screen. # ... # }. pygame.display.flip() tkwin.mainloop() # end while loop