Sguigee
A GUI library for Python
Tuomas Laakkonen, 2015
Available on Github
We're doing the easy version
So why not just use TKInter?
-
Confusing
-
Long
-
Not very Pythonic
Code first
ask questions later
A Simple 'Hello World' Example
import tkinter as tk
t = tk.Tk()
t.title('Hello World')
x = tk.Label(t,
text='Hello World')
x.pack(fill='x', expand=True,
side='left')
t.mainloop()
from sguigee import window, label
with window(title='Hello World'):
label('Hello World')
-
Windows are the base unit
-
A Window divides into Rows
-
Rows Stack
Example: Calculator
from gui import window, row, button, textbox
import operator as op
def make_button(text):
@button(text)
def _button():
output.set(output.get() + text)
return _button
def make_operator(t, op):
@button(t, expand=False, use_monospace_font=True)
def _button():
stack.append(float(output.get()))
output.set('')
stack.append(op)
stack = []
with window(title="Calculator", set_minimum_size=True, can_resize=False):
with row():
output = textbox()
with row():
make_button('1')
make_button('2')
make_button('3')
make_operator('+', op.add)
with row():
make_button('4')
make_button('5')
make_button('6')
make_operator('-', op.sub)
with row():
make_button('7')
make_button('8')
make_button('9')
make_operator('*', op.mul)
with row():
make_button('0')
make_button('.')
make_operator('/', op.truediv)
with row():
@button('Clear')
def clear():
output.set('')
@button('Clear All')
def clear_all():
stack = []
output.set('')
@button('=')
def compute():
while len(stack) >= 2:
op, lhs = stack.pop(), stack.pop()
output.set(op(lhs, float(output.get())))
Rows
- Also use the 'with' statement.
- Simple, no options
-
with row():
pass
Let's combine the two!
with window(title="Title"):
with row():
pass
with row():
pass
But I have no actual content!
Labels
- Like text boxes on word.
- Allow you to put some text in a row.
- Simple:
label("Label Text")
Lets put this all together!
Note: All elements Must be in a row!
Textboxes
- Slightly more complicated!
- Why?
- Textboxes have text which changes!
- So how do we use them?
-
t = textbox()
- How do we get the users input?
-
t.get()
Because code can only execute when the user does something!
So... How do we do anything?
- Well, the problem before was that there was nothing for the user to do.
- Just a textbox for them to fill in.
- We need something they can click...
What's with the '@' thingy?
- Called a decorator.
- Don't worry about it.
- There just the way to do buttons (at this level...)
Now can we put it all together?
Did it work?
-
Yes!
Another thing about text boxes
Some more things: Message boxes
- Used to show, information, errors etc.
- Just like print...
-
show_message('Hello', name)
- Can also customize the title, message type etc.
-
show_message('Aah!', title='Error', type='error')
How do we design a program that uses Sguigee?
Simple steps to follow:
- DRAW YOUR GUI OUT ON PAPER
- PLEASE, PLEASE, PLEASE DO THAT!
- Then, divide it up into rows.
- Translate that into code.
Now go code!
Challenge 1
Write a program to add two numbers.
The program should:
- Have two textboxes, one for each number.
- It should have a button that says 'add'.
- The button should be on a seperate row below the textboxes.
- When you click the button, the program should display a message box showing the sum of the numbers.