How to make random tests with tkinter (part 1)

This is the first part to make a test with tkinter. This will let you do different tests with random values easily.

from PIL import Image, ImageFont, ImageDraw
import os

class App:
    def __init__(self, title="Title", subtitle="A subtitle", image="logo.png", bg="coral"):
        self.title = title
        self.subtitle = subtitle
        self.bg = bg
        self.image = image
        self.size = 600, 400
        self.background = self.bg
        self.img = image
        self.shrink_ratio = 2 # use this in case you need to resize the image in the middle
        self.font = "arial", 38

        # do not need to customize this
        self.filename = "output\\" + self.img[:-4] + "_cover.png" # do not need to change this
        self.fnt = ImageFont.truetype(self.font[0], self.font[1])
        self.fnt2 = ImageFont.truetype("arial", 28)
        self.imgb = Image.new("RGB", (self.size), color=self.background)
        self.create_image()
        
    def create_image(self):
        self.img_middle = self.shrink(self.img, self.shrink_ratio)
        self.draw = ImageDraw.Draw(self.imgb)
        self.subtitle = self.subtitle
        self.draw.text((48, 30), self.title, font=self.fnt)
        self.draw.text((50, 80), self.subtitle, font=self.fnt2)
        self.half4 = (self.middle(self.img_middle,2))
        self.imgb.paste(self.img_middle, self.half4, self.img_middle)
        self.imgb.save(self.filename)
        self.imgb.show()
            
    def shrink(self, link, ratio=2):
        img = Image.open(link)
        w,h = img.size
        w = w // ratio # 2 is 50%
        h = h // ratio
        img = img.resize((w,h), Image.ANTIALIAS)
        return img

    def middle(self, img, m=2):
        return self.size[0] // m -img.size[0] // m, self.size[1] // 2 - img.size[1] // 4

ni = App(title = "How to create a GUI for tests",
         subtitle = "(random tests easily)",
         image = "logo.png",
         bg = "coral")
the result

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.