Showing posts with label use of tkMessageBox with python. Show all posts
Showing posts with label use of tkMessageBox with python. Show all posts

Thursday, August 6, 2015

A simple GUI game with Python Tkinter

The program below demonstrates the  use of tkinter module with tkMessageBox utility and Tkinter. The program will allow you to click on the GUI window of Tkinter and show winner if you click on a specific spot on the window. So lets see it in detail.

After importing the necessary classes we define the main window as root. Then I have created a label which displays the message assigned in text. Then I have created a frame within the root window with color and width,height defined. Then I binded the frame with the mouse button-1 or left click and call the findcurpos function if we left click.
The function defined will take event as argument and print the event.x,event.y, which are current position of the mouse in x and y dimensions. The random.randint(0,130)creates a random set of integer. We compare it with event.x and give a value of y range for simplicity;if the both is true I displayed the message "You Win" in the message box. See the syntax for clarity.

  
from Tkinter import *
import tkMessageBox,random

root = Tk()
Label(root,text="Find Out the Hidden Position").pack()
def findcurpos(event):
       
        print "you clicked at",event.x,event.y
        if event.x in range(random.randint(0,130)):
                if event.y in range(80,100):
                        tkMessageBox.showinfo("You win ","you win!!!")
                        print "you win"
myframe=Frame(root,bg='beige',width=130,height=130)
myframe.bind("<Button-1>",findcurpos)
myframe.pack()
root.mainloop()

Write the program and see what it does. It is nice example of event management. Try "<Return>" instead of "<Button-1>" and see the effects. Play with tkMessageBox, write dir(tkMessageBox) and see what are the processes in their. Play with them. keep updated and subscribe as more of cool programs will be in here.

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...