Saturday, 27 May 2017

Python GUI Programming:

Python provides various options for developing graphical user interfaces (GUI's). 

  • Tkinter
  • PyQt
  • WxPython

Tkinter Programming:

  • Tkinter is the standard GUI library for Python. 
  • Python when combined with Tkinter provides a fast and easy way to create GUI applications. 
  • In-order to work with Tkinter, first we are install Tkinter module.

C:\Python27\Scripts>pip install tkinter


  • After install that module, next we are import that module, how to import that module.
  • in python2.7(python2.x)

>>> import tkinter
>>> 
                                    (or)
>>> import Tkinter
>>> 
  • in python3.5(python3.x)

>>> import tkinter
>>> 
                                     (or)

>>> import Tkinter
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import Tkinter
ImportError: No module named 'Tkinter'

example1:

import Tkinter
top = Tkinter.Tk( )

# Code to add widgets will go here...

top.mainloop()

output:


Example2: button: The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button

import Tkinter
import tkMessageBox

top = Tkinter.Tk( )

def helloCallBack( ):
       tkMessageBox.showinfo( "Hello Python", "Hello World")

B = Tkinter.Button(top, text ="Hello", command = helloCallBack)

B.pack( )
top.mainloop( )

Output:

Example3: canvas: The Canvas is a rectangular area intended for drawing pictures or other complex layouts. You can place graphics, text, widgets or frames on a Canvas.

import Tkinter
import tkMessageBox

top = Tkinter.Tk( )

C = Tkinter.Canvas(top, bg="blue", height=250, width=300)

coord = 10, 50, 240, 210
arc = C.create_arc(coord, start=0, extent=150, fill="red")

C.pack( )
top.mainloop( )

Output:


Example4: check button: The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user can then select one or more options by clicking the button corresponding to each option.

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tkinter.Tk( )
CheckVar1 = IntVar( )
CheckVar2 = IntVar( )
C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C1.pack( )
C2.pack( )
top.mainloop()

Output:




example5: entry field:The Entry widget is used to accept single-line text strings from a user.

from Tkinter import *

top = Tk( )
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)

E1.pack(side = RIGHT)

top.mainloop( )

Output:


Example6: Frame: The Frame widget is very important for the process of grouping and organizing other widgets in a somehow friendly way.

from Tkinter import *

root = Tk()
frame = Frame(root)
frame.pack()

bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

redbutton = Button(frame, text="Red", fg="red")
redbutton.pack( side = LEFT)

greenbutton = Button(frame, text="Brown", fg="brown")
greenbutton.pack( side = LEFT )

bluebutton = Button(frame, text="Blue", fg="blue")
bluebutton.pack( side = LEFT )

blackbutton = Button(bottomframe, text="Black", fg="black")
blackbutton.pack( side = BOTTOM)

root.mainloop( )

Output:


Example7: label: This widget implements a display box where you can place text or images. The text displayed by this widget can be updated at any time you want.

from Tkinter import *

root = Tk( )

var = StringVar()
label = Label( root, textvariable=var, relief=RAISED )

var.set("Hey!? How are you doing?")
label.pack( )
root.mainloop( )

Output:


Example8: Listbox: The Listbox widget is used to display a list of items from which a user can select a number of items.

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tk()

Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")

Lb1.pack( )
top.mainloop( )

Output:


Example9: menubutton: A menubutton is the part of a drop-down menu that stays on the screen all the time. Every menubutton is associated with a Menu widget that can display the choices for that menubutton when the user clicks on it.

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tk()

mb=  Menubutton ( top, text="condiments", relief=RAISED )
mb.grid()
mb.menu  =  Menu ( mb, tearoff = 0 )
mb["menu"]  =  mb.menu
    
mayoVar  = IntVar( )
ketchVar = IntVar( )

mb.menu.add_checkbutton ( label="mayo",
                          variable=mayoVar )
mb.menu.add_checkbutton ( label="ketchup",
                          variable=ketchVar )

mb.pack( )
top.mainloop( )

Output:


Example10: menu: The goal of this widget is to allow us to create all kinds of menus that can be used by our applications. The core functionality provides ways to create three menu types: pop-up, top-level and pull-down.

from Tkinter import *
def donothing( ):
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack( )
   
root = Tk( )
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing)

filemenu.add_separator( )

filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=donothing)

editmenu.add_separator()

editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
editmenu.add_command(label="Delete", command=donothing)
editmenu.add_command(label="Select All", command=donothing)

menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)
root.mainloop( )

Output:





Example11: message:This widget provides a multiline and noneditable object that displays texts, automatically breaking lines and justifying their contents.

from Tkinter import *

root = Tk( )

var = StringVar( )
label = Message( root, textvariable=var, relief=RAISED )

var.set("Hey!? How are you doing?")
label.pack( )
root.mainloop( )

Output:


Example12: Radiobutton: This widget implements a multiple-choice button, which is a way to offer many possible selections to the user and lets user choose only one of them.

from Tkinter import *

def sel( ):
   selection = "You selected the option " + str(var.get())
   label.config(text = selection)

root = Tk( )
var = IntVar( )
R1 = Radiobutton(root, text="Option 1", variable=var, value=1,
                  command=sel)
R1.pack( anchor = W )

R2 = Radiobutton(root, text="Option 2", variable=var, value=2,
                  command=sel)
R2.pack( anchor = W )

R3 = Radiobutton(root, text="Option 3", variable=var, value=3,
                  command=sel)
R3.pack( anchor = W)

label = Label(root)
label.pack( )
root.mainloop( )

Output:



Example13: Scale:The Scale widget provides a graphical slider object that allows you to select values from a specific scale.

from Tkinter import *

def sel( ):
   selection = "Value = " + str(var.get())
   label.config(text = selection)

root = Tk( )
var = DoubleVar()
scale = Scale( root, variable = var )
scale.pack(anchor=CENTER)

button = Button(root, text="Get Scale Value", command=sel)
button.pack(anchor=CENTER)

label = Label(root)
label.pack( )

root.mainloop( )

Output:



Example14: Scrollbar: This widget provides a slide controller that is used to implement vertical scrolled widgets, such as Listbox, Text and Canvas. Note that you can also create horizontal scrollbars on Entry widgets.

from Tkinter import *

root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill=Y )

mylist = Listbox(root, yscrollcommand = scrollbar.set )
for line in range(100):
   mylist.insert(END, "This is line number " + str(line))

mylist.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = mylist.yview )

mainloop( )

Output:



Example15: Text: Text widgets provide advanced capabilities that allow you to edit a multiline text and format the way it has to be displayed, such as changing its color and font.

from Tkinter import *

def onclick():
   pass

root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()

text.tag_add("here", "1.0", "1.4")
text.tag_add("start", "1.8", "1.13")
text.tag_config("here", background="yellow", foreground="blue")
text.tag_config("start", background="black", foreground="green")
root.mainloop( )

Output:

Example16: Top-Level: Toplevel widgets work as windows that are directly managed by the window manager. They do not necessarily have a parent widget on top of them.
Your application can use any number of top-level windows.
from Tkinter import *

root = Tk( )
top = Toplevel( )

top.mainloop( )

Output:


Example17: Spinbox: The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values.

from Tkinter import *

master = Tk( )

w = Spinbox(master, from_=0, to=10)
w.pack( )

mainloop( )

Output:





Example18:PanedWindow: A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically.

from Tkinter import *

m1 = PanedWindow()
m1.pack(fill=BOTH, expand=1)

left = Label(m1, text="left pane")
m1.add(left)

m2 = PanedWindow(m1, orient=VERTICAL)
m1.add(m2)

top = Label(m2, text="top pane")
m2.add(top)

bottom = Label(m2, text="bottom pane")
m2.add(bottom)

mainloop( )

Output:


Example19: LabelFrame: A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts.

from Tkinter import *

root = Tk( )

labelframe = LabelFrame(root, text="This is a LabelFrame")
labelframe.pack(fill="both", expand="yes")
 
left = Label(labelframe, text="Inside the LabelFrame")
left.pack( )
 
root.mainloop( )

Output:



Example20: tkMessageBox: The tkMessageBox module is used to display message boxes in your applications. This module provides a number of functions that you can use to display an appropriate message.

import Tkinter
import tkMessageBox

top = Tkinter.Tk()
def hello():
   tkMessageBox.showinfo("Say Hello", "Hello World")

B1 = Tkinter.Button(top, text = "Say Hello", command = hello)
B1.pack()

top.mainloop()

Output:



No comments:

Post a Comment