pygame and tkinter used together

I'm still in the exploratory phase.

<insert image of Nellie cackaling like a mad scientest as she whips code into run mode>

But apparently you can use Tkinter and pygame together!
That would make constructing a game, paint, everything smoother, if I can do what I think I can.  Pygame could be solely responsible to draw to screen, Tkinter to modifications, menu's, alterations. 






It's just a start, but here's some code for you:



 #!usr/bin/python3
# -*- coding: utf-8 -*-
import pygame as pg
from sys import exit
from time import sleep
import tkinter as tk


class SkullGlobals(object):
    """ class container for globally used variables """
    def __init__(self):
        self.WIDTH = 800
        self.HEIGHT = 800
        self.CENTER_SCREEN_X = 0 + self.WIDTH//12
        self.CENTER_SCREEN_Y = 0 + self.HEIGHT//12
        self.CENTER_SCREEN_H = self.HEIGHT - self.HEIGHT//6
        self.CENTER_SCREEN_W = self.WIDTH - self.WIDTH//6
        self.background_color = (110, 100, 110)
        self.BLUE = (40, 60, 200)
        self.LBLUE = (100, 100, 255)
        self.BORDER = (self.WIDTH + self.HEIGHT)//200
        self.left_y = 0 + self.HEIGHT//100
        self.left_x = 0 + self.WIDTH//100
        self.right_y = self.HEIGHT - self.left_y
        self.right_x = self.WIDTH - self.left_x
        self.small_height = self.left_y * 5
        self.small_width = self.left_x * 10
        self.left_upper_y = 0 + (self.HEIGHT//10)
        self.left_upper_x = 0 + (self.WIDTH//10)
        self.left_upper_right_y = self.HEIGHT - self.BORDER
        self.right_lower_x = self.WIDTH - (self.WIDTH//10)
        self.right_lower_y = self.HEIGHT - (self.HEIGHT//10)
        self.CENTER = (self.WIDTH//2, self.HEIGHT//2)
        self.FULL_SIZE = (self.WIDTH//4 + self.HEIGHT//4)//2
        self.SMALL_SIZE = (self.WIDTH//50 + self.HEIGHT//50)//2
        self.RED = (230, 50, 50)
        self.GREEN = (50, 230, 50)
        self.D_WHITE = (200,200,200)
        self.base_color = 150
        self.menu_bar_color = (90, 100, 80)


class MygewyWindow(tk.Frame):
   """ Create display tkinter frame"""
    def __init__(self, master = None):
       super().__init__(master)
       self.master = master
       self.special_font = ("helevitca", 16, "bold")
       self.special_font_8 = ("helevitca", 8, "bold")
       self.canvas = None
       self.frame_text = None
       self.c2_text = None
       self.m_text = None

    def config_frames(self):
        self.master.title("Tkinter Display")
        self.mycontainer2 = tk.Frame(self.master, bg="old lace")
        self.mycontainer2.pack(side="top", fill="both", expand="yes")
        self.mycontainer = tk.Frame(self.master, bg="floral white")
        self.mycontainer.pack(side="bottom", expand = "no")

class pygameDisplay(object):
   """ Create/Display pygame screen*window*"""
    globals = SkullGlobals()
    screen = pg.display.set_mode((globals.WIDTH//2, globals.HEIGHT//2))
    pg.display.set_caption(('Pygame Display'))
    screen.fill(globals.background_color)
    animation_timer = pg.time.Clock()
    pg.display.flip()

    def get_screen(self):
        return screen



def pygame_loop():
    pg.init()
    globals = SkullGlobals()
    mypygame = pygameDisplay()
    w = globals.WIDTH
    h = globals.HEIGHT
    screen = mypygame.screen
    purple = (200, 20, 200)
    position = (w//4, h//4)
    radius = (w//10 + h//10)//2
    screen.fill(globals.background_color)
    globals = SkullGlobals()
    root = tk.Tk()
    root["background"] = "old lace"
    w = globals.WIDTH
    h = globals.HEIGHT
    root.geometry('{}x{}'.format(w, h))
    myapp = MygewyWindow(root)
    myapp.config_frames()

    running = True

    while running:
        pg.draw.circle(screen, purple, position, radius, 0)
        events = pg.event.get()
        for event in events:
            if event.type == pg.QUIT:
                running = False
                pg.QUIT
                exit(0)
        pg.display.flip()
        root.update()

pygame_loop()





Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame