Python GUI Tk : TK Root Window

Base Windows

 
from Tkinter import *

root = Tk()
    
# create window contents as children to root...
     

root.mainloop()

Bind keyboard action with root

 

from Tkinter import *
import math

root = Tk()     
top = Frame(root)
top.pack(side='top')

hwframe = Frame(top)
hwframe.pack(side='top')
font = 'times 18 bold'
hwtext = Label(hwframe, text='Hello, World!', font=font)
hwtext.pack(side='top', pady=20)

rframe = Frame(top)
rframe.pack(side='top', padx=10, pady=20)
r_label = Label(rframe, text='The sine of')
r_label.pack(side='left')
r = StringVar()
r.set('1.2')   
r_entry = Entry(rframe, width=6, textvariable=r)
r_entry.pack(side='left')

s = StringVar()
def comp_s(event):
    global s
    s.set('%g' % math.sin(float(r.get()))) # construct string

r_entry.bind('<Return>', comp_s)

compute = Label(rframe, text=' equals ')
compute.pack(side='left')

s_label = Label(rframe, textvariable=s, width=12)
s_label.pack(side='left')

def quit(event=None):
    root.destroy()

quit_button = Button(top, text='Goodbye, GUI World!', command=quit,
                     background='yellow', foreground='blue')
quit_button.pack(side='top', pady=5, fill='x')

root.bind('<q>', quit)

root.mainloop()


Set options shared by all Tk widgets(font, colors, etc.)

 

import Tkinter, Pmw, os, sys

def makeGUI(top):
    Tkinter.Label(text='Demonstration of\nfont and color options').pack(pady=5)
    list = Pmw.ScrolledListBox(top,
           label_text = 'my first Pwm widget',
           labelpos = 'n')
    list.pack(pady=5)
    Tkinter.Button(top, text='button 1').pack(pady=5)
    Tkinter.Button(top, text='button 2').pack(pady=5)

    mylist = ('item1','item2','item3')
    for item in mylist:
        list.insert('end', item)

def readOptions():
    if not os.path.isfile('.tkoptions'):
        print 'no file .tkoptions in the current directory'
        return
    global root
    root.option_readfile('.tkoptions')

def addOptions():
    general_font = ('Helvetica', 14, 'bold')
    label_font = ('Times', 24, 'italic')
    listbox_font = ('Helvetica', 20, 'roman bold')

    # check which fonts that are actually available:
    import tkFont
    print 'label_font:',label_font,'\n  realized as',\
          tkFont.Font(font=label_font).actual()
    print 'general_font:',general_font,'\n  realized as',\
          tkFont.Font(font=general_font).actual()
    print 'listbox_font:',listbox_font,'\n  realized as',\
          tkFont.Font(font=listbox_font).actual()

    global root
    root.option_add('*Font', general_font)
    root.option_add('*Foreground', 'black')
    root.option_add('*Label*Font', label_font)
    root.option_add('*Listbox*Font', listbox_font)
    root.option_add('*Listbox*Background', 'green')
    root.option_add('*Listbox*Foreground', 'brown')

root = Tkinter.Tk()
try:
    if sys.argv[1] == 'file':
        readOptions()
    else:
        addOptions()
except:
    print 'fonts.py file|add'; sys.exit(1)

Pmw.initialise(root,useTkOptionDb=1)

root.title('demonstrating font and color settings')

makeGUI(root)
root.mainloop()