0

Is it possible to put all of my code to initalize my tKinter or pygame display into its own class, just to tidy things up. Its all code that gets executed once before I get into my main loop?

Im writing something that uses both a pygame graphic display and a tKinter frame for a GUI. It all works fine, but it seems so messy having all this with no indents right in my main line of code. Nothing clever, just reading the monitor resolution, then using that to adjust the size and location of my PyGame and Tkinter frames.

Ive edited code below to show my main loops, and how i update tkinter and pygame displays. It all works OK.

I just wonder if i could tidy things up by using a class to hold all the buttins etc that i want on the tkinter display.

Wish I could put it all in a class or something. Sorry, i'm a noob, having a blast playing with python!

import random
import math
import os
import pygame
import numpy as np
#from tkinter import *
#from tkinter import ttk
from tkinter import Tk, Button, Label, LabelFrame, PhotoImage, Radiobutton
from tkinter import messagebox, DISABLED, NORMAL, Menu

pygame.init()
infoObject = pygame.display.Info()          #Read video mode full resolution 
pygame.display.set_mode((infoObject.current_w, infoObject.current_h))   #Intialize pygame window to full screen mius 400 on left (to be used for tkinter main window)
SMALL_TEXT = pygame.font.Font('freesansbold.ttf', 12)
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0, 30) #set placement for next window in windows env
gameDisplay = pygame.display.set_mode((infoObject.current_w - 400,infoObject.current_h - 70), pygame.RESIZABLE)
pygame.display.set_caption('Collision Detection')
pygame.font.init() # Set up text ops


root = Tk()
geom = (str(400) + 'x' + str(infoObject.current_h - 70) + '+' + str(infoObject.current_w - 410) + '+' + str(0))
root.geometry(geom)
root.configure(background='gray70')
root.title('Vessel Control')

# Create a couple test buttons and labels on root form, to be updated by code in mainloop


lab = Label(root, text="Don't Push The Button!")
lab.grid(row=0, column=0)
but = Button(root, text="Push Me", command=changebut)
but.grid(row=1, column=0)
posxlab = Label(root, text='USV X Position (m): ')
posxlab.grid(row=2, column=0)


# Here i set up the rest of the junk for my program

# Then i start my own main loop, not usuing tkinters.

# Main loop

while not gameExit:

    #And my full code is in here, a few thousand lines or so



    pygame.display.update()   #update pygame display
    root.update_idletasks()   #process tkinter events
    root.update()             #Update tkinter window
    clock.tick(FRAME_RATE)


pygame.quit()
quit()
Dave
  • 1
  • 2
  • Possible duplicate of [Best way to structure a tkinter application?](https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application) – stovfl Sep 25 '19 at 07:56
  • Have you tried adding `root.mainloop()` at the end of your code? – Legorooj Sep 26 '19 at 06:02
  • Hi Legorooj. I started with a lot of pygame code and after decided it would be a lot nicer interface with a tkinter window. I didnt want to paste all of my code into the tKinter main loop, but found i could use these commands to process events within my existing main loop. pygame.display.update() root.update_idletasks() root.update() – Dave Sep 27 '19 at 15:22

0 Answers0