Tuesday, March 26, 2013

CheckBox of tkinter

Example of using tkinter.Checkbutton in Python. Also demonstrate how to get status of tkinter.Checkbutton.

tkinter.CheckBox
tkinter.CheckBox


#tkinter for Python 3.x
#Tkinter for Python 2.x

import tkinter

def quit():
    global tkTop
    tkTop.destroy()

def setCheckButtonText():
    if varCheckButton.get():
        varLabel.set("checked")
    else:
        varLabel.set("un-checked")  

tkTop = tkinter.Tk()
tkTop.geometry('300x200')

tkButtonQuit = tkinter.Button(
    tkTop,
    text="Quit",
    command=quit)
tkButtonQuit.pack()

varLabel = tkinter.StringVar()
tkLabel = tkinter.Label(textvariable=varLabel)
tkLabel.pack()

varCheckButton = tkinter.IntVar()
tkCheckButton = tkinter.Checkbutton(
    tkTop,
    text="CheckButton",
    variable=varCheckButton,
    command=setCheckButtonText)
tkCheckButton.pack(anchor=tkinter.CENTER)

tkinter.mainloop()


Monday, March 25, 2013

Read keyboard input in Python

char_in = 'a'
print(char_in)
while char_in not in ['Q', 'q']:
    print('Enter Q/q to quit:')
    char_in = input()
    print(char_in)

Read keyboard input in Python
Read keyboard input in Python


Sunday, March 24, 2013

List all built in functions in Python Shell

type the following command in Python Shell to List all built in functions:

>>> dir(__builtins__)

note: There are two leading and trailing underscore characters, "_".

List all built in functions in Python Shell with dir(__builtins__)
List all built in functions in Python Shell with dir(__builtins__)


Wednesday, March 20, 2013

Set font of tkinter.Label

Set font of tkinter.Label
Set font of tkinter.Label


#tkinter for Python 3.x
#Tkinter for Python 2.x

import tkinter

def quit():
    global tkTop
    tkTop.destroy()

def setTextSize(ev=None):
    global tkLabel
    global tkScale
    tkLabel.config(font="Helvetica -%d bold" %tkScale.get())

tkTop = tkinter.Tk()
tkTop.geometry('300x200')

tkButtonQuit = tkinter.Button(tkTop, text="Quit", command=quit)
tkButtonQuit.pack()

tkLabel = tkinter.Label(text="Hello Python")
tkLabel.pack()

tkScale = tkinter.Scale(tkTop, from_=1, to=40, orient=tkinter.HORIZONTAL, command=setTextSize)
tkScale.set(18)
tkScale.pack(anchor=tkinter.CENTER)

tkinter.mainloop()


Implement Scale bar of Tkinter

Scale bar of Tkinter
Scale bar of Tkinter


#tkinter for Python 3.x
#Tkinter for Python 2.x

import tkinter

def quit():
    global tkTop
    tkTop.destroy()

tkTop = tkinter.Tk()
tkTop.geometry('300x200')

tkButtonQuit = tkinter.Button(tkTop, text="Quit", command=quit)
tkButtonQuit.pack()

tkScale = tkinter.Scale(tkTop, from_=1, to=30, orient=tkinter.HORIZONTAL)
tkScale.set(18)
tkScale.pack(anchor=tkinter.CENTER)

tkinter.mainloop()


Define command to destroy window of Python

Define command to destroy window of Python
Define command to destroy window of Python


#tkinter for Python 3.x
#Tkinter for Python 2.x

import tkinter

def quit():
    global tkTop
    tkTop.destroy()

tkTop = tkinter.Tk()
tkTop.geometry('300x200')

tkButtonQuit = tkinter.Button(tkTop, text="Quit", command=quit)
tkButtonQuit.pack()

tkinter.mainloop()


Tuesday, March 19, 2013

Tkinter, set window size using geometry

set window size using geometry
set window size using geometry


#tkinter for Python 3.x
#Tkinter for Python 2.x

import tkinter

tkTop = tkinter.Tk()
tkTop.geometry('300x200')
tkButtonQuit = tkinter.Button(tkTop, text="Quit", command=tkTop.quit)
tkButtonQuit.pack()
tkinter.mainloop()


Simple example using tkinter Button with command

example using tkinter Button with command
example using tkinter Button with command


#tkinter for Python 3.x
#Tkinter for Python 2.x

import tkinter

tkTop = tkinter.Tk()
tkButtonQuit = tkinter.Button(tkTop, text="Quit", command=tkTop.quit)
tkButtonQuit.pack()
tkinter.mainloop()


Simple example using tkinter Label

Simple example using tkinter Label
Simple example using tkinter Label



#tkinter for Python 3.x
#Tkinter for Python 2.x

import tkinter

tkTop = tkinter.Tk()
tkLabel = tkinter.Label(tkTop, text="Hello python.beginner")
tkLabel.pack()
tkinter.mainloop()