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