Friday, September 15, 2017

TKInter GUI - On Mac OS X

I am getting ready to work on a small project. I have chose Python and TKInter for GUI management. I am not that familiar with GUI coding in Python yet and I have been reading where TKInter is notorious looking. So, I decided to perform a little experiment and see how close I could get a GUI to looking presentable enough to use on a Mac. Below is my results. I don't think it looks bad at all.

Now, based on the theory of cross-platform, I should be able to move all this code and it work and look presentable on Windows and Linux as well. That is the subject for another post.



Here is the Python 3 code used to create the GUI:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#
# TKInter GUI Test Example
# By Noble D. Bell
# 8 September 2017 - Rev. 1
#

from tkinter import *
from tkinter import ttk


def newEntry():
    pass

def saveEntry():
    pass

def deleteEntry():
    pass

def searchList():
    pass


# main window container
root = Tk()
root.title("TKInter GUI Test")

# variables
sName    = StringVar()
sPhone   = StringVar()
sSearch  = StringVar()
srchType = StringVar()
lItems   = StringVar()

# widget container
content   = ttk.Frame(root, padding=(3,3,12,12))

# button widgets
btnNew    = ttk.Button(content, text = "New", command = newEntry)
btnSave   = ttk.Button(content, text = "Save", command = saveEntry)
btnDelete = ttk.Button(content, text = "Delete", command = deleteEntry)
btnSearch = ttk.Button(content, text = "Search", command = searchList)

# label widgets
lblName   = ttk.Label(content, text = "Name:")
lblPhone  = ttk.Label(content, text = "Phone:")
lblSearch = ttk.Label(content, text = "Search:")

# entry widgets
txtName   = ttk.Entry(content, textvariable = sName)
txtPhone  = ttk.Entry(content, textvariable = sPhone)
txtSearch = ttk.Entry(content, textvariable = sSearch)

# radio button widgets
rdoName   = ttk.Radiobutton(content, text = "Name", variable = srchType, value = "Name")
rdoPhone  = ttk.Radiobutton(content, text = "Phone", variable = srchType, value = "Phone")

# listbox and scrollbar widgets
lbEntries = Listbox(content, listvariable = lItems, height = 10)
sbResults = ttk.Scrollbar(content, orient = VERTICAL, command = lbEntries.yview)

# bind scrollbar to listbox widget
lbEntries.configure(yscrollcommand = sbResults.set)

# build the gui
content.grid(column=0, row=0, sticky=(N, S, E, W))
lblName.grid(column = 0, row = 0, sticky = (N, W), padx = 5)
txtName.grid(column = 0, row = 1, columnspan = 3, sticky = (N, E, W), pady = 5, padx = 5)
lblPhone.grid(column = 0, row = 2, sticky = (N, W), padx = 5)
txtPhone.grid(column = 0, row = 3, columnspan = 3, sticky = (N, E, W), pady = 5, padx = 5)
lblSearch.grid(column = 0, row = 4, sticky = (N, W), padx = 5)
rdoName.grid(column = 1, row = 4, sticky = (N, W), padx = 5)
rdoPhone.grid(column = 2, row = 4, sticky = (N, W), padx = 5)
txtSearch.grid(column = 0, row = 5, columnspan = 3, sticky = (N, E, W), pady = 5, padx = 5)
lbEntries.grid(column = 0, row = 6, columnspan = 5, sticky = (N, E, W), pady = 5, padx = 5)
btnNew.grid(column = 3, row = 1, padx = 5)
btnSave.grid(column = 3, row = 2, padx = 5)
btnDelete.grid(column = 3, row = 3, padx = 5)
btnSearch.grid(column = 3, row = 5, padx = 5)

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

content.columnconfigure(0, weight=3)
content.columnconfigure(1, weight=3)
content.columnconfigure(2, weight=3)
content.columnconfigure(3, weight=1)
content.columnconfigure(4, weight=1)
content.rowconfigure(1, weight=1)

root.mainloop()

Wednesday, June 14, 2017

Handling Sprite Sheets in Python/Pygame 3

# This class handles sprite sheets
# This was taken from www.scriptefun.com/transcript-2-using
# sprite-sheets-and-drawing-the-background.
# I've added some code to fail if the file wasn't found..
# Note: When calling images_at the rect is the format:
# (x, y, x + offset, y + offset)
# Modifed by Noble Bell for Python 3


import pygame

class spritesheet(object):
    def __init__(self, filename):
        self.sheet = pygame.image.load(filename).convert()

    # Load a specific image from a specific rectangle
    def image_at(self, rectangle, colorkey = None):
        "Loads image from x,y,x+offset,y+offset"
        rect = pygame.Rect(rectangle)
        image = pygame.Surface(rect.size).convert()
        image.blit(self.sheet, (0, 0), rect)
        if colorkey is not None:
            if colorkey is -1:
                colorkey = image.get_at((0,0))
            image.set_colorkey(colorkey, pygame.RLEACCEL)
        return image

    # Load a whole bunch of images and return them as a list
    def images_at(self, rects, colorkey = None):
        "Loads multiple images, supply a list of coordinates" 
        return [self.image_at(rect, colorkey) for rect in rects]

    # Load a whole strip of images
    def load_strip(self, rect, image_count, colorkey = None):
        "Loads a strip of images and returns them as a list"
        tups = [(rect[0]+rect[2]*x, rect[1], rect[2], rect[3])
                for x in range(image_count)]
        return self.images_at(tups, colorkey)