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

Friday, July 31, 2015

Python Tkinter Button,Label and Text Example

Python Tkinter has all the facilities a desktop GUI application will need, we will discuss button, label and text maker in Tkinter here. The generic declaration format to create  the three is like below -

    label=Lable(mainwindow,option=......)
text=Text(mainwindow,option=......)
         button=Button(mainwindow,options=.....)

Button is the name of the function/class you can see from the declaration. In this we have created a Button in the mainwindow e.g root I we have created in my previous blog.Options can be anything like fg - Normal foreground, bg - background ,font - text font used in the button, and many more like height, width , command,text and many more.

Lets see the creations of those with example -

from Tkinter import *
import tkMessageBox

class JustExample:
        def __init__(self,master):
                button=Button(master,text="Click Me",command=self.clicked).pack()
                text=Text(master,fg="blue",bg='beige').pack()
                label=Label(master,text="The Label").pack(
side=TOP)
        def clicked(self):
                tkMessageBox.showinfo("Hello Python","Nearly There")
               
root=Tk()
c=JustExample(root)
root.mainloop()


I created a class to show the procedure of creating button,label and text box. Besides importing the Tkinter I have also imported tkMessageBox module.The JustExample class is initialized with the master window or root window, and then I have cretaed one button with text 'click me' and the command which called the function in the class clicked(). You can see the text option bg and fg which says you will write blue text in beige background. The label has the option pack with side=TOP which says it will be on the top of the window. The output will look something like below -






Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...