Friday, September 15, 2017

Same TKInter GUI - On Mac OS X

Here is a screenshot of the same GUI as below but on a Mac OS X box. The only difference in code is a small change I made to the padding around the widgets in order to tighten up the space on the outside of the window. I am by no means a Mac superstar but to me this GUI looks pretty good, clean, and usable.

Saturday, September 9, 2017

A GUI In TKInter For Python

This is a GUI example created in TKInter for Python 3. It is being displayed from Linux Mint:



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, August 23, 2017

WxPython Flies Again!!

This is something I found that is very promising for Python 3 users. It certainly looks good to me and it is free unlike the QT stuff I have seen.

https://wxpython.org/


Tuesday, August 22, 2017

Sorting a List of Tuples by Second Element

For a project that I am working on I needed to be able to sort a list of scores and initials. This is what I came up with:
1
2
3
4
# Sorting a list of tuples by second element in tuple. 

scores = sorted([('plyr1', 350), ('plyr2', 425), ('plyr3', 1250), ('plyr4', 500)], key = lambda x: x[1], reverse = True) 
print (scores)

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)