473,701 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python and Tkinter

kaarthikeyapreyan
107 New Member
Beginners Game- Tic-Tac-Toe
My first attempt after learning Tkinter
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import tkFont
  3. class myApp:
  4.   """
  5.   Defining The Geometry of the GUI
  6.   And the variables Used In the The methods
  7.  
  8.   """
  9.  
  10.   def  __init__(self,parent):
  11.  
  12.  
  13.     self.parent_instance=parent
  14.     self.parent_instance.geometry("650x400")
  15.  
  16.     self.myContainer1=Frame(parent)
  17.     self.myContainer1.pack()
  18.  
  19.     #Setting the Dimension constraints for few buttons
  20.  
  21.     button_width = 6      
  22.     button_padx = "2m"    
  23.     button_pady = "1m"    
  24.  
  25.     buttons_frame_padx =  "3m"   
  26.     buttons_frame_pady =  "2m"          
  27.     buttons_frame_ipadx = "3m"   
  28.     buttons_frame_ipady = "1m"   
  29.  
  30.     # label frame
  31.     self.label_frame = Frame(self.myContainer1) 
  32.     self.label_frame.pack( 
  33.       side=TOP,   
  34.       ipadx=buttons_frame_ipadx,   
  35.       ipady=buttons_frame_ipady,   
  36.       padx=buttons_frame_padx,    
  37.       pady=buttons_frame_pady,    
  38.       )    
  39.  
  40.     # middle frame
  41.     self.middle_frame = Frame(self.myContainer1) 
  42.     self.middle_frame.pack(side=TOP,
  43.       ipadx=buttons_frame_ipadx,   
  44.       ipady=buttons_frame_ipady,   
  45.       padx=buttons_frame_padx,    
  46.       pady=buttons_frame_pady,    
  47.  
  48.       fill=BOTH, 
  49.       expand=YES,
  50.       )  ###
  51.  
  52.     # bottom frame
  53.     self.bottom_frame = Frame(self.myContainer1, relief=GROOVE,
  54.       height=50
  55.       ) ###   
  56.     self.bottom_frame.pack(side=TOP,
  57.       ipadx=buttons_frame_ipadx,   
  58.       ipady=buttons_frame_ipady,   
  59.       padx=buttons_frame_padx,    
  60.       pady=buttons_frame_pady,    
  61.  
  62.       fill=BOTH, 
  63.       expand=YES,
  64.       )  ###
  65.  
  66.     # left_frame
  67.     self.left_frame = Frame(self.middle_frame,
  68.       borderwidth=10,  relief=RIDGE,
  69.       height=250, 
  70.       width=250, 
  71.       ) ###
  72.     self.left_frame.grid(sticky=W+E+S+N)
  73.     self.left_frame.pack(side=LEFT,
  74.       ipadx=buttons_frame_ipadx,   
  75.       ipady=buttons_frame_ipady,   
  76.       padx=buttons_frame_padx,    
  77.       pady=buttons_frame_pady,    
  78.  
  79.       fill=BOTH, 
  80.       expand=YES,
  81.       )  ###
  82.  
  83.  
  84.     ### right_frame 
  85.     self.right_frame = Frame(self.middle_frame,
  86.       #borderwidth=5,  relief=RIDGE,
  87.       width=50,
  88.       )
  89.     self.right_frame.pack(side=RIGHT,
  90.       ipadx=buttons_frame_ipadx,   
  91.       ipady=buttons_frame_ipady,   
  92.       padx=buttons_frame_padx,    
  93.       pady=buttons_frame_pady,    
  94.  
  95.       fill=BOTH, 
  96.       expand=YES,
  97.       )  ###
  98.  
  99.     ### Defining Working Variables
  100.  
  101.     self.plstate=1   ### State of the player in COMPUTER mode
  102.     self.statevalue=0  ### State of the player in player mode
  103.     self.winvar=0 ### Determine if a player has won
  104.     self.dvar=0 ### Detrmine if the match is a draw
  105.     self.svar=0### Determine if the result is declared
  106.     self.clist=[0,0,0,0,0,0,0,0,0] ### Operating List
  107.     self.gstart=0 ### to start the game
  108.     self.nstart = 1 ### to disallow only start 
  109.     self.implist=[]
  110.     self.visualist=[]
  111.     self.logicalgroups=[[0,4,8],[2,4,6],[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8]] ### Possible combinational List hardcoded...:-(
  112.     self.helv20 = tkFont.Font ( family="Helvetica",size=20, weight="bold" ) ### Font defined to display 
  113.  
  114.   def action_button(self):
  115.  
  116.     """
  117.     Grid Layout for the buttons in action
  118.  
  119.     """    
  120.     self.button0 = Button( self.left_frame,width=2,height=2)
  121.     self.button0.grid( row = 0, column = 0, sticky = W+E+N+S )  
  122.     self.button0.bind("<Button-1>",
  123.                       lambda
  124.                       event,arg1=0,arg2='M':
  125.                       self.buttonclick(event,arg1,arg2))
  126.  
  127.     self.button1 = Button( self.left_frame,width=2,height=2)
  128.     self.button1.grid( row = 0, column = 1,  sticky = W+E+N+S )
  129.     self.button1.bind("<Button-1>",
  130.                       lambda
  131.                       event,arg1=1,arg2='M':
  132.                       self.buttonclick(event,arg1,arg2))
  133.  
  134.     self.button2 = Button(self.left_frame,width=2,height=2)
  135.     self.button2.grid( row = 0, column = 2, sticky = W+E+N+S )
  136.     self.button2.bind("<Button-1>",
  137.                       lambda
  138.                       event,arg1=2,arg2='M':
  139.                       self.buttonclick(event,arg1,arg2))
  140.  
  141.     self.button3 = Button(self.left_frame,width=2,height=2)
  142.     self.button3.grid( row = 1, column = 0, sticky = W+E+N+S )
  143.     self.button3.bind("<Button-1>",
  144.                       lambda
  145.                       event,arg1=3,arg2='M':
  146.                       self.buttonclick(event,arg1,arg2))
  147.  
  148.     self.button4 = Button( self.left_frame,width=2,height=2)
  149.     self.button4.grid( row = 1, column = 1, sticky = W+E+N+S )
  150.     self.button4.bind("<Button-1>",
  151.                       lambda
  152.                       event,arg1=4,arg2='M':
  153.                       self.buttonclick(event,arg1,arg2))
  154.  
  155.     self.button5 = Button( self.left_frame,width=2,height=2)
  156.     self.button5.grid( row = 1, column = 2,  sticky = W+E+N+S )
  157.     self.button5.bind("<Button-1>",
  158.                       lambda
  159.                       event,arg1=5,arg2='M':
  160.                       self.buttonclick(event,arg1,arg2))
  161.  
  162.     self.button6 = Button( self.left_frame,width=2,height=2)
  163.     self.button6.grid( row = 2, column = 0, sticky = W+E+N+S )
  164.     self.button6.bind("<Button-1>",
  165.                       lambda
  166.                       event,arg1=6,arg2='M':
  167.                       self.buttonclick(event,arg1,arg2))
  168.  
  169.     self.button7 = Button( self.left_frame,width=2,height=2)
  170.     self.button7.grid( row = 2, column = 1, sticky = W+E+N+S )
  171.     self.button7.bind("<Button-1>",
  172.                       lambda
  173.                       event,arg1=7,arg2='M':
  174.                       self.buttonclick(event,arg1,arg2))
  175.  
  176.     self.button8 = Button( self.left_frame,width=2,height=2)
  177.     self.button8.grid( row = 2, column = 2, sticky = W+E+N+S )  
  178.     self.button8.bind("<Button-1>",
  179.                       lambda
  180.                       event,arg1=8,arg2='M':
  181.                       self.buttonclick(event,arg1,arg2))
  182.  
  183.     self.stare(1)
  184.  
  185.   def stare(self,val):
  186.     for id in range(9):
  187.       if val == 1 :
  188.         refresh="self.button%d.configure(text='!',background='grey',state=ACTIVE)"%id
  189.         exec(refresh)
  190.       else:
  191.         refresh="self.button%d.configure(text='',background='white',state=ACTIVE)"%id
  192.         exec(refresh)
  193.  
  194.   def menu_button(self):
  195.  
  196.     """
  197.     Buttons for the control actions
  198.  
  199.     """
  200.     ###Defining The New Button
  201.     self.newbutton=Button(self.right_frame)
  202.     self.newbutton.configure(background='tan',text='NEW GAME',width=10)
  203.     self.newbutton.grid(row=0,column=0)
  204.     self.newbutton.bind("<Button-1>",self.newbuttonclick)
  205.  
  206.     ###Defining The Close Button
  207.     self.closebutton=Button(self.right_frame)
  208.     self.closebutton.configure(background='tan',text='CLOSE GAME',width=10)
  209.     self.closebutton.grid(row=0,column=1)
  210.     self.closebutton.bind("<Button-1>",self.closebuttonclick)
  211.  
  212.     ###Defining The start Button
  213.     self.startbutton=Button(self.right_frame)
  214.     self.startbutton.configure(background='tan',text='START',width=10)
  215.     self.startbutton.grid(row=10,column=0)
  216.     self.startbutton.bind("<Button-1>",self.startbuttonclick)
  217.  
  218.     ###Defining The Status Button    
  219.     self.statusbutton1=Button(self.right_frame,width=10,relief=GROOVE)
  220.     self.statusbutton1.configure(background='blue',text='PLAYER ONE TURN',width=15)
  221.     self.statusbutton1.grid(row=10,column=1)
  222.  
  223.     self.cplabel=Label(self.right_frame,text="PLAYER VS",fg='blue')
  224.     self.cplabel.grid(row=1,column=0)
  225.     self.mlabel=Label(self.right_frame,text="TO-START",fg='blue')
  226.     self.mlabel.grid(row=4,column=1)
  227.     self.gslabel=Label(self.right_frame,text="MODES",fg='blue')
  228.     self.gslabel.grid(row=1,column=2)
  229.  
  230.     ###Defining The Radio Button
  231.     self.rvar=IntVar()
  232.     self.avar=IntVar() #To start
  233.     self.mvar=IntVar()
  234.  
  235.     self.RB1=Radiobutton(self.right_frame,text='PLAYER',value=0,variable=self.rvar)
  236.     self.RB1.grid(row=2,column=0)
  237.     self.RB2=Radiobutton(self.right_frame,text='COMPUTER',value=1,variable=self.rvar)
  238.     self.RB2.grid(row=3,column=0)
  239.  
  240.     self.RB3=Radiobutton(self.right_frame,text='PLAYER',value=0,variable=self.avar)
  241.     self.RB3.grid(row=5,column=1)
  242.     self.RB4=Radiobutton(self.right_frame,text='COMPUTER',value=1,variable=self.avar)
  243.     self.RB4.grid(row=6,column=1)
  244.  
  245.     self.RB5=Radiobutton(self.right_frame,text='EASY',value=0,variable=self.mvar)
  246.     self.RB5.grid(row=2,column=2)
  247.     self.RB6=Radiobutton(self.right_frame,text='NORMAL',value=1,variable=self.mvar)
  248.     self.RB6.grid(row=3,column=2)
  249.     self.RB7=Radiobutton(self.right_frame,text='HARD',value=2,variable=self.mvar)
  250.     self.RB7.grid(row=4,column=2)
  251.  
  252.     self.rvar.set(0)
  253.     self.avar.set(0)
  254.     self.mvar.set(0)
  255.  
  256.   def label(self):
  257.  
  258.     """
  259.     Display Constraints of various labels 
  260.  
  261.     """
  262.  
  263.     g_label=Label(self.label_frame,text="Tic Tac Toe",font=self.helv20,fg='blue')
  264.     g_label.pack(side=TOP,anchor=CENTER)
  265.  
  266.     s_label=Label(self.bottom_frame,text='COURTESY: QUINTEGRA SOLUTIONS',fg='grey')
  267.     s_label.pack(anchor=CENTER)
  268.  
  269.  
  270.   def newbuttonclick(self,event):
  271.  
  272.     """
  273.     Event That happens if I click 
  274.     The New Game button
  275.  
  276.     """
  277.     print 'NEW GAME'
  278.     for i in range(9):
  279.         refresh="self.button%d.configure(text='',background='white',state=ACTIVE)"%i
  280.         exec(refresh)
  281.  
  282.     ### Initialising all variables 
  283.     self.statusbutton1.configure(background='blue',text='PLAYER ONE TURN',width=15)
  284.     self.clist=[0,0,0,0,0,0,0,0,0]
  285.     self.nstart = 1
  286.     toplay = self.avar.get()
  287.     if toplay == 1:
  288.       self.plstate=1
  289.     else:
  290.       self.plstate=2
  291.  
  292.     self.statevalue=0
  293.     self.dvar=0
  294.     self.svar=0
  295.     self.winvar=0
  296.     self.gstart=0
  297.     self.implist=[]
  298.  
  299.     self.RB1.config(state=ACTIVE)
  300.     self.RB2.config(state=ACTIVE)
  301.     self.RB3.config(state=ACTIVE)
  302.     self.RB4.config(state=ACTIVE)
  303.     self.RB5.config(state=ACTIVE)
  304.     self.RB6.config(state=ACTIVE)
  305.     self.RB7.config(state=ACTIVE)
  306.  
  307.     self.stare(1)
  308.     ###
  309.  
  310.   def closebuttonclick(self,event):
  311.  
  312.     """
  313.     Event That happens if I click 
  314.     The close button
  315.  
  316.     """
  317.     print 'CLOSE GAME'
  318.     self.parent_instance.destroy()
  319.  
  320.  
  321.   def startbuttonclick(self,event):
  322.  
  323.     if self.nstart == 1 :
  324.       self.gstart = 1
  325.       self.stare(0)
  326.  
  327.       if self.rvar.get() == 1:
  328.         self.RB1.config(state=DISABLED)
  329.       else:
  330.         self.RB2.config(state=DISABLED)
  331.         self.RB3.config(state=DISABLED)
  332.         self.RB4.config(state=DISABLED)
  333.         self.RB5.config(state=DISABLED)
  334.         self.RB6.config(state=DISABLED)
  335.         self.RB7.config(state=DISABLED)
  336.  
  337.       if self.avar.get() == 1:
  338.         self.plstate=2   ### State of the player in COMPUTER mode
  339.         self.playmygame()
  340.         self.RB3.config(state=DISABLED)
  341.       else:
  342.         self.plstate=1
  343.         self.RB4.config(state=DISABLED)
  344.  
  345.       if self.mvar.get() == 0 :
  346.         self.RB6.config(state=DISABLED)
  347.         self.RB7.config(state=DISABLED)
  348.       elif self.mvar.get() == 1 :
  349.         self.RB5.config(state=DISABLED)
  350.         self.RB7.config(state=DISABLED)
  351.       else:
  352.         self.RB5.config(state=DISABLED)
  353.         self.RB6.config(state=DISABLED)
  354.  
  355.       self.nstart = 0
  356.  
  357.  
  358.  
  359.   def buttonclick(self,event,arg1,arg2): 
  360.  
  361.     """
  362.     For Every Action There is an 
  363.     Equal and opposite reaction
  364.     Defining Button Click Events In player mode and COMPUTER mode
  365.  
  366.     """
  367.  
  368.     if self.gstart == 1 :   
  369.       if self.rvar.get() == 0:
  370.         if self.statevalue==0:
  371.           if self.clist[arg1] == 0:  
  372.             if self.clist[arg1] != 3:
  373.               mse_cmd="self.button%d.configure(text='O',background='blue')"%arg1
  374.               exec(mse_cmd)
  375.               self.statusbutton1.configure(text='PLAYER TWO TURN',background='red')
  376.               self.clist[arg1]=1
  377.               self.statevalue=1
  378.               self.checkwin()
  379.  
  380.         else :
  381.           if self.clist[arg1] == 0 :
  382.             if self.clist[arg1] != 3:
  383.               mse_cmd="self.button%d.configure(text='X',background='red')"%arg1
  384.               exec(mse_cmd)
  385.               self.statusbutton1.configure(text='PLAYER ONE TURN',background='blue',width=15)
  386.               self.clist[arg1]=2
  387.               self.statevalue=0
  388.               self.checkwin()
  389.  
  390.       else:
  391.  
  392.         if self.plstate == 1:
  393.           if self.clist[arg1] == 0:
  394.             mse_cmd="self.button%d.configure(text='O',background='blue')"%arg1
  395.             exec(mse_cmd)
  396.             self.clist[arg1]=1
  397.             ### COMPUTER 's Turn
  398.             self.checkwin()
  399.             if self.dvar != 1 :
  400.               self.plstate=2
  401.               self.statusbutton1.configure(text='COMPUTER TURN',background='red')
  402.         if self.plstate == 2 :
  403.           self.win()
  404.           self.caution()
  405.           if self.mvar.get() == 2 :
  406.             self.playmygame()
  407.           elif self.mvar.get() == 1:
  408.             self.normal()
  409.           else :
  410.             self.easy()
  411.           self.checkwin()
  412.  
  413.   ###Check for  winning possiblity for both the players
  414.   ###And then implement it
  415.  
  416.   def win(self):
  417.  
  418.     """
  419.     Check the winning Possiblities in COMPUTER mode
  420.  
  421.     """
  422.  
  423.     for subgroup in self.logicalgroups:
  424.       if self.plstate==2:  
  425.         if self.clist[subgroup[0]] == 2 and self.clist[subgroup[1]] == 2:
  426.           if self.clist[subgroup[2]] == 0:
  427.             pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[2]
  428.             exec(pce_cmd)
  429.             self.clist[subgroup[2]]=2
  430.             self.plstate=1
  431.       if self.plstate==2:  
  432.         if self.clist[subgroup[0]] == 2 and self.clist[subgroup[2]] == 2:
  433.           if self.clist[subgroup[1]] == 0:
  434.             pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[1]
  435.             exec(pce_cmd)
  436.             self.clist[subgroup[1]]=2
  437.             self.plstate=1
  438.       if self.plstate==2:  
  439.         if self.clist[subgroup[1]] == 2 and self.clist[subgroup[2]] == 2:
  440.           if self.clist[subgroup[0]]==0:
  441.             pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[0]
  442.             exec(pce_cmd)
  443.             self.clist[subgroup[0]]=2
  444.             self.plstate=1
  445.       self.statusbutton1.configure(text='PLAYER ONE TURN',background='blue')
  446.  
  447.  
  448.   def caution(self):
  449.  
  450.     """
  451.     Check failures the in COMPUTER mode
  452.     """
  453.     if self.mvar.get() == 0 :
  454.       pass
  455.     else :
  456.       for subgroup in self.logicalgroups:
  457.         if self.plstate==2:
  458.           if self.clist[subgroup[0]] == 1 and self.clist[subgroup[1]] == 1:
  459.             if self.clist[subgroup[2]] == 0:
  460.               pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[2]
  461.               exec(pce_cmd)
  462.               self.clist[subgroup[2]]=2
  463.               self.plstate=1
  464.         if self.plstate==2:
  465.           if self.clist[subgroup[0]] == 1 and self.clist[subgroup[2]] == 1:
  466.             if self.clist[subgroup[1]]==0:
  467.               pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[1]
  468.               exec(pce_cmd)
  469.               self.clist[subgroup[1]]=2
  470.               self.plstate=1
  471.         if self.plstate==2:
  472.           if self.clist[subgroup[1]] == 1 and self.clist[subgroup[2]] == 1:
  473.             if self.clist[subgroup[0]]==0:
  474.               pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[0]
  475.               exec(pce_cmd)
  476.               self.clist[subgroup[0]]=2
  477.               self.plstate=1
  478.  
  479.         self.statusbutton1.configure(text='PLAYER ONE TURN',background='blue')
  480.  
  481.   ##########################################
  482.   def playmygame(self):
  483.  
  484.     for index,implements in enumerate(self.clist):
  485.       if implements == 2:
  486.         self.implist.append(index)
  487.  
  488.     if self.clist[4] == 0 :
  489.       if self.plstate == 2 :
  490.         self.button4.configure(text='X',background='red')
  491.         self.clist[4] =2
  492.         self.plstate=1
  493.  
  494.     self.proximity()
  495.  
  496.   def proximity(self):
  497.  
  498.     store=[]
  499.     proxlist=[]
  500.     for index,val in enumerate(self.clist):
  501.       if val == 1 :
  502.         store.append(index)
  503.  
  504.  
  505.     for sub in self.logicalgroups:
  506.       for index,enter in enumerate(store):
  507.         if enter in sub :
  508.           if sub[0] == enter :
  509.             proxlist.append(sub[1])
  510.  
  511.           elif sub[1] == enter :
  512.             proxlist.append(sub[0])
  513.             proxlist.append(sub[2])
  514.  
  515.           elif sub[2] == enter :
  516.             proxlist.append(sub[1])
  517.  
  518.     for proxies in proxlist:
  519.       if self.plstate == 2 : 
  520.         if self.clist[proxies] == 0 :
  521.           self.clist[proxies] = 2
  522.           pr_cmd="self.button%d.configure(text='X',background='red')"%proxies
  523.           exec(pr_cmd)
  524.           self.plstate = 1
  525.   ##########################################    
  526.   def normal(self):
  527.  
  528.     for index,implements in enumerate(self.clist):
  529.       if implements == 2:
  530.         self.implist.append(index)
  531.  
  532.     self.reason()
  533.     for index,j in enumerate(self.reasoninglist):
  534.       if j == 0 :
  535.         self.visualise(self.logicalgroups[index])
  536.         for index1,sgp in enumerate(self.visualist):
  537.           if sgp == 0:
  538.             if self.plstate == 2 :
  539.               temp=self.logicalgroups[index]
  540.               pce_cmd="self.button%d.configure(text='X',background='red')"%temp[index1]
  541.               exec(pce_cmd)
  542.               self.clist[temp[index1]]=2
  543.               self.plstate=1
  544.  
  545.     if self.plstate == 2 :
  546.       for index2,nonval in enumerate(self.clist):
  547.         if nonval == 0 :
  548.           if self.plstate == 2 :
  549.               nce_cmd="self.button%d.configure(text='X',background='red')"%index2
  550.               exec(nce_cmd)
  551.               self.clist[index2]=2
  552.               self.plstate=1
  553.  
  554.  
  555.   def visualise(self,Slist):
  556.     self.visualist=[]
  557.     for i in Slist:
  558.       self.visualist.append(self.clist[i])
  559.  
  560.   def reason(self):
  561.  
  562.     self.reasoninglist=[]
  563.     for group in self.logicalgroups:
  564.       self.visualise(group)
  565.       if 1 in self.visualist:  
  566.         self.reasoninglist.append('R')
  567.       else :
  568.         self.reasoninglist.append(0)
  569.   ##########################################
  570.   def easy(self):
  571.  
  572.     for index,unmarked in enumerate(self.logicalgroups):
  573.       if self.clist[unmarked[0]] == 0:
  574.         if self.plstate == 2 :
  575.           cmd="self.button%d.configure(text='X',background='red')"%unmarked[0]
  576.           exec(cmd)
  577.           self.clist[unmarked[0]]=2
  578.           self.plstate=1
  579.       if self.clist[unmarked[1]] == 0:
  580.         if self.plstate == 2 :
  581.           cmd="self.button%d.configure(text='X',background='red')"%unmarked[1]
  582.           exec(cmd)
  583.           self.clist[unmarked[1]]=2
  584.           self.plstate=1
  585.       if self.clist[unmarked[2]] == 0:
  586.         if self.plstate == 2 :
  587.           cmd="self.button%d.configure(text='X',background='red')"%unmarked[2]
  588.           exec(cmd)
  589.           self.clist[unmarked[2]]=2
  590.           self.plstate=1
  591.  
  592.   ##########################################
  593.  
  594.   def statewin(self,val):
  595.  
  596.     """
  597.     Terminal Output of Who wins
  598.  
  599.     """
  600.     if self.svar == 0:
  601.       if val == 1 :
  602.         print 'PLAYER ONE WINS'
  603.       elif self.rvar.get() == 1:
  604.         print 'COMPUTER WINS'
  605.       else:
  606.         print 'PLAYER TWO WINS'
  607.  
  608.     self.svar=1
  609.  
  610.   def checkwin(self):
  611.  
  612.     """
  613.     Algorithm to check the winning Possiblities
  614.  
  615.     """
  616.  
  617.     for subgroup in self.logicalgroups:
  618.       if self.clist[subgroup[0]] == 1:
  619.         if self.clist[subgroup[1]] == 1:
  620.           if self.clist[subgroup[2]] == 1:
  621.             if self.svar == 0:
  622.               self.winvar = 1
  623.               self.statewin(1)
  624.               self.fil(self.winvar)
  625.  
  626.       if self.clist[subgroup[0]] == 2:
  627.         if self.clist[subgroup[1]] == 2:
  628.           if self.clist[subgroup[2]] == 2:
  629.             if self.svar ==0 :
  630.               self.winvar = 2
  631.               self.statewin(2)
  632.               self.fil(self.winvar)
  633.  
  634.     self.draw()
  635.  
  636.   def fil(self,IDN):
  637.  
  638.     """
  639.     To avoid Playing After a person Wins the game
  640.     """
  641.  
  642.     for index,ID in enumerate(self.clist):
  643.       if ID == 0 :
  644.         cbs="self.button%d.configure(text='--',background='white',state=DISABLED)"%index
  645.         exec(cbs)
  646.         self.clist[index]  = 3
  647.  
  648.     if IDN==1:
  649.       Player='PLAYER ONE'
  650.     elif self.rvar.get() == 1:
  651.       Player='COMPUTER'
  652.     else:
  653.       Player='PLAYER TWO'
  654.  
  655.     ###Display The Winner In a seperate Window  
  656.     msg_box=Toplevel()
  657.     msg_box.title("Winner")
  658.     msg_box.geometry("350x50")
  659.     wd="Display=Label(msg_box,text='%s WINS',font=self.helv20)"%(Player)
  660.     exec(wd)
  661.     Display.pack()
  662.  
  663.   def draw(self):
  664.  
  665.     """
  666.     To Declare a Draw
  667.  
  668.     """
  669.     if not 0 in self.clist:
  670.       if not 3 in self.clist:
  671.         if self.winvar == 0:
  672.           print 'MATCH DRAW'
  673.           msg_box=Toplevel()
  674.           msg_box.title("MATCH RESULT")
  675.           msg_box.geometry("300x50")
  676.           Display=Label(msg_box,text='MATCH DRAWN',font=self.helv20)
  677.           Display.pack()
  678.           self.dvar=1 ### Setting the draw flag
  679.  
  680. root = Tk()
  681. root.title("TIC TAC TOE")
  682. myapp = myApp(root)
  683. myapp.action_button()
  684. myapp.menu_button()
  685. myapp.label()
  686. root.mainloop()
Apr 3 '08 #1
1 5660
bvdet
2,851 Recognized Expert Moderator Specialist
kaarthikeyaprey an,

I recently started learning Tkinter. I was looking through Python answers and ran across this thread. Well done!

BV
Mar 4 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3846
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or less - during my own installation of Python 2.3 on Fedora Core 1 Linux for a friend of mine). Anyway, HTH, L.
14
3662
by: Jeffrey Barish | last post by:
Since upgrading to python 2.4 yesterday, I am no longer able to use Tkinter. From http://www.python.org/moin/TkInter I learned that I need to edit Modules/Setup to include Tkinter during the make. However, it isn't clear to me what modifications to make. I am supposed to specify directories in which needed files can be found. I found the Tcl/Tk libraries in /usr/local/lib/python2.4/lib-tk, but I can't find the Tcl/Tk headers. The...
10
3689
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
6
2446
by: Peter Milliken | last post by:
Hi, I (think I have :-)) installed Python on my Pocket PC (obtained from http://fore.validus.com/~kashtan/). There were Tkinter binaries with it so I installed those as well. When I attempt to run the most simplistic of python programs using Tkinter, I get an error message stating that Python can't find any tkinter module. Any ideas what I have done wrong anybody?
1
2230
by: John Chambers | last post by:
Sp my latest adventure is attempting to use python's Tkinter module on a few machines. On my PB (OSX 10.3.9), I got the following confusing results: /Users/jc: python Python 2.3 (#1, Sep 13 2003, 00:49:11) on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import Tkinter Traceback (most recent call last): File "<stdin>", line 1, in ?
44
3693
by: jiang.haiyun | last post by:
Now i began to learn GUI programming. There are so many choices of GUI in the python world, wxPython, pyGTK, PyQT, Tkinter, .etc, it's difficult for a novice to decide, however. Can you draw a comparison among them on easy coding, pythonish design, beautiful and generous looking, powerful development toolkit, and sufficient documentation, .etc. It's helpful for a GUI beginner. Thank you.
40
2726
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I'm really annoyed at Python - and not for the reasons already mentioned on this list. Everyone know that programming is supposed to be a dark art, nearly impossible to learn. Computer code is supposed to be something impossible to read to the common person and yet reveal their secrets to the initiated - just remember the code displayed in the Matrix... Python takes all of that away. It is just too readable. Even beginners can...
13
3361
by: Daniel Fetchinson | last post by:
Was looking at PEP 3108, http://www.python.org/dev/peps/pep-3108/ , Is it just me or others also think that it would be a major loss to remove tkinter from the python core? PEP 3108 starts off with: Each module to be removed needs to have a justification as to why it should no longer be distributed with Python. then goes on with,
8
3286
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to my pc. But, it is not getting installed and is failing by throwing the below errors. Should i need to configure / install any specific files for resolving this issue ?
2
2176
by: Dudeja, Rajat | last post by:
Hi, So, now I've finally started using Eclipse and PyDev as an IDE for my GUI Application. I just wrote some sample programs as an hands on. Now I would like to take up Tkinter. I'm using Active State Python version 2.5 and found that there is not Tkinter and Tk module in it. To use Tkinter do I actually require Tk installed on my machine? Please suggest and where can I find both these modules?
0
8649
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9233
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9085
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6575
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5905
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4665
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3104
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.