Showing posts with label tkinter. Show all posts
Showing posts with label tkinter. Show all posts

Monday, July 13, 2009

Tkinter tidbit: login prompt

I wrote a tkinter login promt that looks like this:



It creates the window, adds the labels, entries and button, connects them with focus / confirm commands and then centres the screen. When the credentials are submitted, it calls the login method.

The source for it is:

Sunday, July 12, 2009

Python tkinter tidbit: centred window

This little application simply creates a window and centres it on the screen (gist):


1 #! /usr/bin/env python3
2
3 from tkinter import *
4 from tkinter.ttk import *
5
6 def centred_win(root, width=400, height=300):
7 screen_width = root.winfo_screenwidth()
8 screen_height = root.winfo_screenheight()
9 x = (screen_width/2) - (width/2)
10 y = (screen_height/2) - (height/2)
11 root.geometry('%dx%d+%d+%d' % (width, height, x, y))
12
13 root = Tk()
14 centred_win(root, 500, 300)
15 root.mainloop()

Tuesday, July 7, 2009

TkDocs first example in python 3.1

The following code does exactly the same as Mark Roseman's TkDocs A First (Real) Example. To match the other language examples, this code has no structuring. The Python way would obviously be to do this in a more OOP way.

So, here is the code (gist):

#! /usr/bin/env python3

# include tkinter packages
from tkinter import *
from tkinter.ttk import *

# create tk root class
root = Tk()

# create frame, add title and setup grid
frame = Frame(root, padding='3 3 12 12')
frame.master.title('Feet to meters')
frame.grid(column=0, row=0, sticky=NW)
frame.grid_columnconfigure(0, weight=1)
frame.grid_rowconfigure(0, weight=1)

# text entry
frame.feet = Entry(frame, width=7)
frame.feet.grid(column=2, row=1, sticky=W)

# calculated meters label
frame.meters = Label(frame, text='')
frame.meters.grid(column=2, row=2, sticky=W)

# calculate callback
def calculate(*e):
try:
frame.meters.config(
text = str(0.3048 * int(frame.feet.get()) *
10000.0/10000.0))
except:
frame.meters.config(text="")

# calculate button
frame.calc = Button(frame, text='Calculate', command = calculate)
frame.calc.grid(column=3, row=3, sticky=W)

# feet label
frame.flbl = Label(frame, text='feet')
frame.flbl.grid(column=3, row=1, sticky=W)

# is eq label
frame.islbl = Label(frame, text='is equivalent to')
frame.islbl.grid(column=1, row=2, sticky=E)

# meters label
frame.mlbl = Label(frame, text='meters')
frame.mlbl.grid(column=3, row=2, sticky=W)

# distance the widgets a bit
for w in frame.children.values():
w.grid(padx=5, pady=5)

# set focus to feet entry
frame.feet.focus()

# bind to calculate
frame.calc.bind("<Return>", calculate)

# enter main loop
root.mainloop()

Saturday, June 27, 2009

Python 3 Tkinter base window

Here is my Python 3 Tkinter base window code. It create a root window, puts a menubar with a quit option on it, adds a status bar and maximises the window. The code is meant to be self explanatory, so don't search for comments.

Syntax highlighted version here.


#! /usr/bin/env python3

from tkinter import Tk, Frame, Canvas, Menu, Label, SUNKEN, BOTTOM, W, X

class root_win(Tk):
def __init__(self):
Tk.__init__(self)

def run(self):
self.build_window()
self.mainloop()

def build_window(self):
self.title('My prototype window')

self.build_menu()
self.build_statusbar()
self.resize()

def build_menu(self):
self.menu = Menu()
self.config(menu = self.menu)

self.m_file = Menu(self.menu)
self.m_file.add_command(
label='Quit', command = self.destroy)
self.menu.add_cascade(label='File', menu = self.m_file)

def build_statusbar(self):
self.statusbar = Label(borderwidth=1, relief=SUNKEN, anchor=W)
self.statusbar.config( text = 'Started..')
self.statusbar.pack(fill=X, side=BOTTOM)

def resize(self):
w, h = self.winfo_screenwidth(), self.winfo_screenheight()
self.geometry("%dx%d+0+0" % (w, h))

gui = root_win()
gui.run()